ASP.Net: HttpModules
An HTTP module is code that gets called for every request or response routed to your application. This code is registered with the server and can subscribe to events within the request-and-response pipeline. You can write HTTP modules to manage custom security levels, do custom logging, or execute similar tasks.
At the most fundamental level, ASP.Net HTTP Modules are classes which implement the System.Web.IHttpModule interface.
public interface IHttpModule
{ void Dispose(); void Init(HttpApplication context);
}
using System;
using System.Web;
using System.Diagnostics;
public class LoggingModule: IHttpModule
{
public LoggingModule()
{
}
public String ModuleName
{
get { return "LoggingModule"; }
}
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += (new EventHandler(this.Application_BeginRequest));
context.EndRequest += (new EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (VirtualPathUtility.GetExtension(context.Request.FilePath) == ".aspx")
{
EventLog eLog = new EventLog();
eLog.Source = "Applicaiton";
eLog.WriteEntry("Begin .aspx request :: " +
DateTime.Now.ToLongDateString() + " :: " +
context.Server.MachineName,
EventLogEntryType.Information);
}
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (VirtualPathUtility.GetExtension(context.Request.FilePath) == ".aspx")
{
EventLog eLog = new EventLog();
eLog.Source = "Applicaiton";
eLog.WriteEntry("End .aspx request :: " +
DateTime.Now.ToLongDateString() + " :: " +
context.Server.MachineName,
EventLogEntryType.Information);
}
}
}
In IIS 6
The following list shows the key events exposed by the HttpApplication object.
Event When It's Called
BeginRequest Before request processing starts
AuthenticateRequest To authenticate client
AuthorizeRequest To perform access check
ResolveRequestCache To get response from cache
AcquireRequestState To load session state
PreRequestHandlerExecute Before request sent to handler
PostRequestHandlerExecute After request sent to handler
ReleaseRequestState To store session state
UpdateRequestCache To update response cache
EndRequest After processing ends
PreSendRequestHeaders Before buffered response headers sent
PreSendRequestContent Before buffered response body sent
One of the most common operations when intercepting an HTTP request is to terminate the request. This is very common in custom authentication modules added to ASP.NET. In this case, the HttpApplication class has a method called CompleteRequest that is called for finishing the request. Calling CompleteRequest with the appropriate status codes can terminate the request.
ASP.NET HTTP Modules can be used to further extend the HTTP pipeline. Many of the services provided by the runtime are indeed implemented as HTTP modules. Common uses for creating custom modules include custom authentication and authorization schemes, in addition to any other filtering services that your application needs.
At the most fundamental level, ASP.Net HTTP Modules are classes which implement the System.Web.IHttpModule interface.
public interface IHttpModule
{ void Dispose(); void Init(HttpApplication context);
}
using System;
using System.Web;
using System.Diagnostics;
public class LoggingModule: IHttpModule
{
public LoggingModule()
{
}
public String ModuleName
{
get { return "LoggingModule"; }
}
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += (new EventHandler(this.Application_BeginRequest));
context.EndRequest += (new EventHandler(this.Application_EndRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (VirtualPathUtility.GetExtension(context.Request.FilePath) == ".aspx")
{
EventLog eLog = new EventLog();
eLog.Source = "Applicaiton";
eLog.WriteEntry("Begin .aspx request :: " +
DateTime.Now.ToLongDateString() + " :: " +
context.Server.MachineName,
EventLogEntryType.Information);
}
}
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
if (VirtualPathUtility.GetExtension(context.Request.FilePath) == ".aspx")
{
EventLog eLog = new EventLog();
eLog.Source = "Applicaiton";
eLog.WriteEntry("End .aspx request :: " +
DateTime.Now.ToLongDateString() + " :: " +
context.Server.MachineName,
EventLogEntryType.Information);
}
}
}
Registering the Handler by Using Web.config
In IIS 6
<configuration>
<system.Web>
<httpModules>
<add name="LoggingModule" type="LoggingModule" />
</httpModules>
</system.Web>
</configuration>
In IIS 7<configuration>
<system.WebServer>
<modules>
<add name="LoggingModule" type="LoggingModule" />
</modules>
</system.WebServer>
</configuration>
The following list shows the key events exposed by the HttpApplication object.
Event When It's Called
BeginRequest Before request processing starts
AuthenticateRequest To authenticate client
AuthorizeRequest To perform access check
ResolveRequestCache To get response from cache
AcquireRequestState To load session state
PreRequestHandlerExecute Before request sent to handler
PostRequestHandlerExecute After request sent to handler
ReleaseRequestState To store session state
UpdateRequestCache To update response cache
EndRequest After processing ends
PreSendRequestHeaders Before buffered response headers sent
PreSendRequestContent Before buffered response body sent
One of the most common operations when intercepting an HTTP request is to terminate the request. This is very common in custom authentication modules added to ASP.NET. In this case, the HttpApplication class has a method called CompleteRequest that is called for finishing the request. Calling CompleteRequest with the appropriate status codes can terminate the request.
ASP.NET HTTP Modules can be used to further extend the HTTP pipeline. Many of the services provided by the runtime are indeed implemented as HTTP modules. Common uses for creating custom modules include custom authentication and authorization schemes, in addition to any other filtering services that your application needs.
Comments
Post a Comment