Niraj Bhatt – Architect's Blog

Ruminations on .NET, Architecture & Design

ASP.NET Modules Overview

I hit across this while explaining WIF in one of my talks. I was showing how events of an ASP.NET module ‘WSFederationAuthenticationModule’ can be used to extract claims and populate a custom ClaimsPrincipal object. To do so I was tapping into the module event with Global.asax file

void WSFederationAuthenticationModule_SecurityTokenValidated( object sender, SecurityTokenValidatedEventArgs e ) { … }

Few participants (non ASP.NET background I guess) weren’t aware as what I was up to and asked me if I can blog around it. So here I am with few basics on ASP.NET modules.

ASP.NET module is a component that extends ASP.NET’s HTTP pipeline. This component is simply a class that implements an interface – System.Web.IHttpModule. These modules are designed to intercept every request flowing in/out of ASP.NET Handlers (e.g. System.Web.UI.Page). Modules generally hook to Application level events (via HttpApplication class which is an application level HttpHandler) like BeginRequest to add features like authentication, caching, etc. An important thing to note here is that modules are maintained as a collection inside HttpApplication class and if you are handling an Application level event like BeginRequest – both at a module level and in Global.asax (inherited version of HttpApplication class specific to your application), it’s the module which is invoked first.

Now many a times while creating your custom ASP.NET Module you may want to trigger some internal events, so that site developers can extend module functionality in the way they like. Defining an event inside ASP.NET module is similar to what you would have done in C# code. What many are not aware is how to consume them? Take a look at below code:

public class CustomModule : IHttpModule
public event EventHandler RequestProcessed;

public void Init( HttpApplication application )
application.BeginRequest += CustomModule_BeginRequest;

void CustomModule_BeginRequest( object sender, EventArgs e )
// N.B. Application Object is the one which invokes Module's Begin Request
// You can also use HttpContext.Current here
var application = sender as HttpApplication;

// Process Request

if( RequestProcessed != null )
RequestProcessed(this, EventArgs.Empty); // Notify Others

public void Dispose() { ... }

So the question is how to subscribe for RequestProcessed event. There are 2 ways to go about doing that –

1) Manually via HttpApplication class – As earlier Global.asax is your application specific inherited version of HttpApplication class. HttpApplication class has a property Modules which contains the list of Http modules. Just pull out your module and subscribe to its event
var module = Modules["CustomModule"] as CustomModule;
if(module != null) module.RequestProcessed += YourHandlerName;

2) Rely on ASP.NET for auto event wiring. And that’s what we did with WIF WSFederationAuthenticationModule. To hook with SecurityTokenValidated we just added a method in Global.asax – ModuleName_EventName (WSFederationAuthenticationModule_SecurityTokenValidated)

A difficult part though for latter is to discover the events of an existing module. Object Explorer has been my only guide for this in past.

Hope this helps🙂 .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: