vendredi 31 août 2018

Using reflection to change HttpRequest _form filed

I am trying to change an HttpRequest's Forms parameters at run-time using reflection, i am aware that this is like shaking hands with the devil, and aren't at all recommended, but i have to.

I am following the way suggested in this answer, but the GetValue method always returns null.

  public static HttpContext InjectFakePostParamsInHttpContext(HttpContext context, NameValueCollection postParameters)
    {
        var field = context.Request.GetType().GetField("_form", BindingFlags.NonPublic | BindingFlags.Instance);            
        var requestForm = (NameValueCollection)field.GetValue(context.Request);
        if (requestForm != null)
        {
            PropertyInfo readable = requestForm.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
            if (readable != null)
            {
                readable.SetValue(requestForm, false, null);
                foreach (string postParameter in postParameters)
                {
                    requestForm[postParameter] = postParameters[postParameter];
                }

                readable.SetValue(requestForm, true, null);
            }
        }
        return context;
    }

This seems to work perfectly when using "_queryString", but when using "_form" i am getting a null requestForm!

What i am missing here?

Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire