ASP tutorial. NET IHttpModule Template user Login Code instance
void Application_acquirerequeststate (Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
User user = (user) application.context.session["user"]; Get user
if (user = null)
{
Application.context.server.transfer ("WebForm1.aspx");
String requesturl = Application.request.url.tostring ();
String requestpage = requesturl.substring (Requesturl.lastindexof ('/') + 1);
If the requested page is not a login page, it is just redirected to the login page.
if (requestpage!=) Login.aspx ")
//Application.server.transfer ("ihttphandlertext/login/login.aspx");
Application.response.redirect ("login/login.aspx");
}
Else
{
Application.response.write (String.Format) ("Welcome!") {0}! ", User.Name));
}
}
%>
Creating a custom HTTP Module
IHttpModule interface
Provides module initialization and disposition events to the implementation class.
Namespaces: system.web
Assembly: system.web (in System.Web.dll)
Grammar
public interface IHttpModule
public interface IHttpModule
public class Helloworldmodule:ihttpmodule
{
Public HelloWorldModule ()
{
}
public string ModuleName
{
get {return "HelloWorldModule";}
}
//In the INIT function, register for HttpApplication
//events by adding yo ur handlers.
public void init (HttpApplication application)
{
& nbsp; application.beginrequest + =
(new EventHandler (This.application_beginrequest));
application.endrequest + =
(new EventHandler (This.application_endrequest));
}
private void Application_BeginRequest (object source,
EventArgs e)
{
//Create HttpApplication and HttpContext objec TS to access
/Request and Response properties.
HttpApplication application = (HttpApplication) source;
HttpContext context = Application.context;
context.response.write ("
helloworldmodule:beginning of Request
</font> }
private void Application_EndRequest (Object source, EventArgs e)
{
HttpApplication application = (HttpApplication) source;
HttpContext context = Application.context;
Context.Response.Write ("Helloworldmodule:end of request</font>}
public void Dispose ()
{
}
}