Tuesday, December 24, 2013

Using HTTP Modules


HTTP Modules are assemblies which can be called during every http request.

This comes in very handy when there is a need to intercept every request that comes into the Web application.

This is basically a simple class that is derived from IHttpModule interface.

Here is the sample code. This will be compiled to a DLL file.


public class MyClass : IHttpModule
{
        /// <summary>
        /// This is the disposing function.
        /// </summary>
        void IHttpModule.Dispose()
        {

        }

        /// <summary>
        /// This is the initialization function.
        /// </summary>
        /// <param name="context">This is the httpApplication context.</param>
        void IHttpModule.Init(HttpApplication httpApp)
        {
            httpApp.BeginRequest += new EventHandler(context_BeginRequest);
        }

        /// <summary>
        /// This is where the Begin Request Action is taking place.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication httpApp = (HttpApplication)sender;
            RequestManipulation(httpApp);
        }

        /// <summary>
        /// This is the core function which tries to alter the request.
        /// </summary>
        private void RequestManipulation(HttpApplication httpApp)
        {
                    AlterNameValueCollection(httpApp);
        }

       /// <summary>
        /// Writes details of the Form NameValueCollection in the REQUEST object.
        /// </summary>
        /// <param name="httpApp">This is the input HttpApplication context from which the request header details will be written out.</param>
        private void AlterNameValueCollection(HttpApplication httpApp)
        {
            NameValueCollection postInfo = httpApp.Request.Form;
            System.Reflection.PropertyInfo prop = httpApp.Request.Form.GetType().GetProperty("IsReadOnly", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            // Set the property false (writable)
            prop.SetValue(httpApp.Request.Form, false, null);
            foreach (String eachKey in postInfo.AllKeys)
            {
                string towrite = string.Format("Key : {0} - Value : {1}", eachKey, httpApp.Request.Form[eachKey]);
                Helper.WriteinFile(towrite);
                if (eachKey == "AuthorName")
                {
                    Helper.WriteinFile("Before Changing AuthorName value....");
                    string oldValue = httpApp.Request.Form[eachKey].ToString();
                    httpApp.Request.Form[eachKey] = "MyClass Success. The old value '" + oldValue + "' was modified by the MyClass dll." ;
                    Helper.WriteinFile("Just now changed the AuthorName value.");
                }
            }
        }
}

After this code is compiled, copy the DLL file to the "bin" folder of the ASP.NET application on which the requests should be intercepted.

This DLL will be loaded into memory by using the following entries in the "Web.Config" file.

  <system.webServer>
    <modules>
      <add name="MyClass" type="MyNamespace.MyClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=77ced88f9fdf21cb" preCondition="managedHandler,runtimeVersionv4.0" />
    </modules>
  </system.webServer>
 
This DLL can also be installed in the "Global Assembly Cache" and the above entries should load it automatically.

Hope this blog helps.

Happy coding !

Cheers
Adam
 
 




 
 

All Blogs so far ...