Skip to content

Instantly share code, notes, and snippets.

@gsherman
Created January 24, 2011 18:57
Show Gist options
  • Save gsherman/793723 to your computer and use it in GitHub Desktop.
Save gsherman/793723 to your computer and use it in GitHub Desktop.
an example of an HttpModule that html encodes form data
using System;
using System.Collections.Specialized;
using System.Reflection;
using System.Web;
public class PrevalidationSanitizer : System.Web.IHttpModule
{
private HttpApplication httpApp;
public void Init(HttpApplication httpApp)
{
this.httpApp = httpApp;
httpApp.PreRequestHandlerExecute += new System.EventHandler(PreRequestHandlerExecute_Event);
}
public void Dispose() { }
public void PreRequestHandlerExecute_Event(object sender, System.EventArgs args)
{
NameValueCollection form = httpApp.Request.Form;
Type type = form.GetType();
PropertyInfo prop = type.GetProperty("IsReadOnly", BindingFlags.Instance
| BindingFlags.IgnoreCase | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
prop.SetValue(form, false, null);
if (httpApp.Request.RequestType == "POST" != null && httpApp.Request.Form["testinput"])
httpApp.Request.Form.Set("testinput", httpApp.Server.HtmlEncode(httpApp.Request.Form["testinput"]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment