Created
January 24, 2011 18:57
-
-
Save gsherman/793723 to your computer and use it in GitHub Desktop.
an example of an HttpModule that html encodes form data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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