Asp tutorial. net ihttpmodule template user login code instance
Void application_acquirerequeststate (object source, eventargs e)
{
Httpapplication application = (httpapplication) source;
User = (user) application. context. session ["user"]; // obtain the 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 logon page, you have just redirected to the logon page.
If (requestpage! = "Login. aspx ")
// Application. server. transfer ("ihttphandlertext/login. aspx ");
Application. response. redirect ("login/login. aspx ");
}
Else
{
Application. response. write (string. format ("Welcome! {0 }! ", User. name ));
}
}
%>
Create a custom http Module
Ihttpmodule Interface
Provides module initialization and event handling to implementation classes.
Namespace: system. web
Assembly: system. web (in system. web. dll)
Syntax
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 your handlers.
Public void init (httpapplication application)
{
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 objects 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 ()
{
}
}