Attention: We are retiring the IIS.NET Community Blogs. Learn more >

Tip #43: Did you know... Way you register your IHttpModule depends on the pipeline in which an AppPool for your module will run?

To get your custom IHttpModule up and running you need three simple things:

  • Source code
  • Compiled and GAC’ed DLL
  • Registration with IIS config system

Suppose, you have a simple module that raises an event with HttpContext as an argument before authentication happens, on BeginRequest:

public void Init(HttpApplication context)
{
    context.BeginRequest += new System.EventHandler(context_BeginRequest);
}

public void Dispose() { }

public class PreAuthEventArgs : EventArgs
{
    public HttpContext httpContext;

    public PreAuthEventArgs(HttpContext context)
    {
        httpContext = context;
    }
}

public delegate void PreAuthRequestEventHandler(object sender, PreAuthEventArgs e);
public event PreAuthRequestEventHandler PreAuthEvent;

void context_BeginRequest(object sender, System.EventArgs e)
{
    if (PreAuthEvent != null)
    {
        PreAuthEvent(this, new PreAuthEventArgs(HttpContext.Current));
    }
}
}

Suppose, a DLL for your module is gac’ed every time it is compiled. You can accomplish this by gac’ing the target DLL in Post-build event from the Properties of your solution/project.

clip_image002

Now to register your module, you need to verify in which pipeline it will be executed.

For IIS6 and IIS7 Classic Mode register your module in web.config in the following manner:

<configuration>
  <system.web>
    <httpModules>
      <add name="AuthenticationCheckerModule " type="AuthenticationCheckerModule "/>
    </httpModules>
  </system.web>
</configuration>

For IIS7 in Integrated Mode do the following:

<configuration>
  <system.webServer>
    <modules>
      <add name="AuthenticationCheckerModule " type="AuthenticationCheckerModule "/>
    </modules>
  </system.webServer>
</configuration>

For more detailed information refer to this MSDN article:

Walkthrough: Creating and Registering a Custom HTTP Module
http://msdn.microsoft.com/en-us/library/ms227673.aspx

Kateryna Rohonyan
SDET, IIS Team

No Comments