Compile a custom HTTP Module

Source: Internet
Author: User

The general process of writing a custom HTTP module is:

    • ImplementationIhttpmoduleInterface.
    • ProcessingInitMethod and register it with the event you need.
    • Process events.
    • If you must clear the configurations, you can also choose to implementDisposeMethod.
    • Register the module in the web. config file.

Event

The following table shows events that can be intercepted using ASP. NET during request processing. All events are listed in the order of occurrence.

The first list displays the events generated before processing the request.

    • Beginrequest. This event indicates that this is a new request; each request must generate this event.

    • Authenticaterequest. This event indicates that the configured authentication mechanism has verified the request. Attach this event to the filter to ensure that the request has passed authentication.

    • Authorizerequest. AndAuthenticaterequestIn the same way, this event marks another step in the request processing process and the request has been authorized.

    • ResolverequestcacheThe output cache module uses this event to simplify the processing of cached requests.

    • Acquirerequeststate. This event indicates that each request status should be obtained.

    • Prerequesthandlerexecute. This event indicates request processingProgramTo be executed. This is the last event that you can participate in before calling the HTTP processing program of this request.

The next list shows the events generated after processing the request. These events are listed in the order of occurrence:

    • Postrequesthandlerexecute. This event indicates that the HTTP processing program has completed processing the request.

    • Releaserequeststate. This event indicates that the request status should be stored because the application has processed the request.

    • Updaterequestcache. This event marksCodeProcessing is completed. You can add files to the ASP. NET cache.

    • Endrequest. This event indicates that all requests have been processed. This is the last event called at the end of the application.

In addition, the following three pre-processing events can be triggered in an uncertain order:

    • Presendrequestheaders. This event indicates that the HTTP header will be sent to the client. Therefore, you can add, delete, or modify header information before sending the message.

    • Presendrequestcontent. This event indicates that the content will be sent to the client. This provides an opportunity to modify the content before sending the message.

    • Error. This event indicates an unhandled exception.

The following example shows how a request is intercepted after it passes the authentication of the ASP. NET Runtime Library. NameUserloggerWhen the sample module is initializedOnauthenticateThe member function is connectedAuthenticaterequestEvent. Each time a new request is authenticatedOnauthenticateFunction. In this example,OnauthenticateThe function will record the name of the user who has passed the authenticationIntercepting FilterIn the event log of the mode application.

 Using  System;
Using System. Web;
Using System. Security. Principal;
Using System. diagnostics;
Public   Class Userlogmodule: ihttpmodule { Private Httpapplication httpapp;
Public   Void Init (httpapplication httpapp) {This. Httpapp=Httpapp; httpapp. authenticaterequest+ = NewEventhandler (onauthentication );}
Void Onauthentication ( Object Sender, eventargs)
{Httpapplication Application=(Httpapplication) sender;
Httpresponse response=Application. Context. response; windowsidentity identity=(Windowsidentity) application. Context. User. Identity; loguser (identity. Name );} Private   Void Loguser (string name) {EventLog log= NewEventLog (); log. Source= "Intercepting Filter Pattern"; Log. writeentry (name, eventlogentrytype. Information );} Public   Void Dispose () {} } A
The sample module must be added to the web. config file so that the ASP. NET Runtime Library can recognize this module. The configuration file changed for the userlogmodule sample module is as follows:
 
<Httpmodules> <Add name = "userlogmodule" type = "userlogmodule, ifilter"/>  

Example

The following is an example of a built-in screenshot filter in Microsoft. NET:

    • defaultauthenticationmodule . This filter ensures that the authentication object appears in the httpcontext object.

    • fileauthorizationmodule . This filter verifies that a remote user has the Microsoft Windows NT permission required to access the requested file.

    • formsauthenticationmodule . This filter enables ASP. NET Applications to use form verification.

    • passportauthenticationmodule . This filter provides a package for the passportauthentication service for passport authentication.

    • sessionstatemodule . This filter provides session status services for applications.

    • urlauthorizationmodule . This filter provides URL-based authorization services to allow or deny access to a specified URL.

    • windowsauthenticationmodule . This filter enables ASP. NET Applications to use the authentication mechanism of Microsoft Windows or Internet Information Service (IIS.

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.