Httpmodule, filtering and interfering with ASP. NET event processing)

Source: Internet
Author: User

HttpmoduleThroughHttpapplicationObject event processingHTTPProcess MPs queue impact. These events must beHttpmoduleOfInitMethod, including:Beginrequest,Authenticaterequest,Authorizerequest,Resolverequestcache,Acquirerequeststate,Prerequesthandlerexecute,Postrequesthandlerexecute,Releaserequeststate,Updaterequestcache,Endrequest.

Using System;
Using System. Web;
Using System. Security. Principal;

Namespace Xumh
{
///   <Summary>
///  
/// Implement httpmodule:
/// 1. Write class implementation ihttpmodule
/// 2. compile it as a Class Library: CSC/T: Library testhttpmodule. CS
/// 3. To make your web page use it, you must register it in Web. config.
///   <Httpmodules>  
///   <Add name = "xumhhttpmodule" type = "xumh. testhttpmodule, testhttpmodule"/>
///   <Add name = "whatever name" type = "Space. Class Name, DLL file without extension/>
///   </Httpmodules>
/// 4. This httpmodule handles registration. To facilitate testing, anonymous access needs to be disabled as follows:
/// <Authorization>
/// <Deny users = "? "/>
/// </Authorization>
///   </Summary>
Public   Class Testhttpmodule: ihttpmodule
{
Public   Void Dispose ()
{
Throw   New Notimplementedexception ();
}

Public VoidInit (httpapplication context)
{//We can register many events.
Context. authenticaterequest+ = NewEventhandler (context_authenticaterequest );
}

// Authenticaterequest
Void Context_authenticaterequest ( Object Sender, eventargs E)
{
Httpapplication app = (Httpapplication) sender;
Httpcontext Context = (Httpcontext) app. context;
If (App. request [ " Userid " ] =   Null   | App. request [ " Password " ] = Null )
{
Context. response. Write ( " The user name or password is empty. Verification Failed! " );
App. response. End ();
}
//
String Userid = App. request [ " Userid " ]. Tostring ();
String Password = App. request [ " Password " ]. Tostring ();
String [] Roles = Authenticateandgetroles (userid, password ); // Get user permission list
If (Roles = Null   | Roles. getlength ( 0 ) = 0 )
{
App. response. Write ( " Incorrect username or password. Verification Failed! " );
App. completerequest (); // Terminate an HTTP request
}
Genericidentity identity =   New Genericidentity (userid, " Customauthentication " );
Context. User =   New Genericprincipal (identity, roles );

}

// Authenticateandgetroles
Private   String [] Authenticateandgetroles ( String Userid, String Password)
{
String [] Roles =   Null ;
If (Userid. Equals ( " Xuminghui " ) && Password. Equals ( " 1234 " ))
{
Roles =   New   String [ 1 ];
Roles [ 0 ] =   " Administrator " ;
}
Else   If (Userid. Equals ( " Haohao " ) && Password. Equals ( " 1017 " ))
{
Roles =   New   String [ 1 ];
Roles [ 0 ] =   " User " ;
}
Return Roles;
}

}

}

For detailed procedures, see

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.