There are two ways to mount the httphandler object: one is to directly modify it. machine. in this way, Mount all Asp.net programs on the local machine to the httphandler object, and modify the web in the virtual directory. config file. This scheme will only affect the Asp.net program in the virtual directory. The second method is used here. Add the following description to the Web. config file.
<Add verb = "*" Path = ". Ho" type = "c64.httphandler. hellohandler, hellohanlder"/>
The path part is set to inform the httpapplication object of the file extension corresponding to the httphandler object. The first part of type is the class name and namespace, and the latter part is the Assembly file name. In addition, to enable aspnet_isapi.dll to accept the requirements of the. Ho file, you must set the table for setting the extension of the virtual directory content in IIS.
By default, when the httphandler object is called, the session object is not initialized to the available State. If the httphandler object needs to use the session object, the irequiressessionstate interface must be implemented simultaneously, no implementation is required. The following program implements the hellohandler class source code of the irequiressessionstate interface.
using System;using System.Web;using System.Web.SessionState;namespace c64.HttpHandler{public class HelloHandler : IHttpHandler,IRequiresSessionState{#region IHttpHandler Memberspublic void ProcessRequest(HttpContext context){context.Response.ContentType = "text/html";context.Response.Write("
To test this program, we can place a button on webform. When the user presses the button, it stores a string in session ["user"], and then asks. the result is displayed in the ho file.
Implement httpmodule
Unlike the loading time of the httphandler object, the httpmodule object is loaded when the httpapplication object is initialized. This time is when the first user accesses this virtual directory after the local machine is started. There are not many opportunities for designers to write httpmodule objects, or even fewer opportunities to write httphandler objects. The following is a hellomodule object to demonstrate how to write an httpmodule object.
using System;using System.Web;using System.Web.SessionState;namespace HttpModule{public class HelloModule : IHttpModule,IRequiresSessionState{#region IHttpModule Memberspublic void Init(HttpApplication context){context.Application["HELLO"] = "I am Hello Module!";context.Application[GetHashCode().ToString()] = 123;}public void Dispose(){}#endregion}}
This hellomodule object simply puts a string in the application's State object in the same way as loading the httphandler object. We can modify mechine. config file or web in a virtual directory. config file to load it. The second solution is used here. add the following description to the config file.
<Httpmodules>
<Add name = "hellomodule" type = "httpmodule. httpmodule, hellomodule"/>
</Httpmodules>
To test this hellomodule object, you only need to place a button with webform. When the user presses the button, the string put by the hellomodule object can be retrieved from the application state object, is the execution result of the hellomodule object.
Ihttpmodule implements permission Control
There are many ways to implement a system permission management module in the General Office. Here we will discuss with you how to implement the permission management module in the pipeline model.
ASP. NET Framework processes an HTTP request:
Httprequest --> inetinfo.exe --> response --> aspnet_wp.exe --> httpruntime --> httpapplication factory --> httpapplication --> httpmodule --> httphandler factory --> httphandler. processrequest ()
ASP. the net request processing process is based on the pipeline model, which consists of multiple httpmodules and httphandler, Asp.. Net passes the HTTP request to each httpmodule in the pipeline in sequence, and is finally processed by httphandler. After the processing is completed, it passes through the HTTP module in the pipeline again and returns the result to the client.
We can intervene in the request processing process in each httpmodule.
Note: During HTTP request processing, only one httphandler can be called, but multiple httpmodules can be called.
How to Implement permission control:
You can use this feature to control page access permissions. When a user requests a page, the user can make a judgment in httpmodule. If the user does not specify a role, the user is denied access.
First, let's take a look at the ihttpmodule interface:
The ihttpmodule provides the implementation class with module initialization and event handling.
Ihttpmodule contains two methods:
Public void Init (httpapplication context );
Public void dispose ();
Init (): This method accepts an httpapplication object. httpapplication represents the current application, and we need to register the event where the httpapplication object is exposed to the client in this method. It can be seen that this method is only used to register events, and the actual event processing program requires us to write another method.
The entire process is well understood:
1. when the first resource of the site is accessed, Asp. net will create an httpapplication class instance, which represents the site application, at the same time will create all in the web. the module instance that has been registered in config.
2. Call the init () method of modul when creating a module instance.
3. register the events exposed by httpapplication that you want to respond to in the init () method. (Only simple registration of methods is required. The actual method needs to be written separately ).
4. httpapplication triggers various events in its application cycle.
5. When an event is triggered, call the method that the module has registered in its Init () method.
Dispose (): it can be cleaned up before garbage collection.
Steps:
First create a login page login. aspx; main code:
Protected void btnlogin_click (Object sender, eventargs E)
{
If (this.txt username. Text. Trim ()! = "" & This.txt PWD. Text. Trim ()! = "")
{
String url = request. querystring ["returnurl"];
// Add an authentication ticket
Formsauthentication.redirectfromloginpage(this.txt username. Text, false );
If (URL! = NULL)
{
Response. Redirect (URL );
}
Response. Redirect ("index. aspx ");
}
}
Create a class to implement the ihttpmodule interface: Test. CS
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
/// <Summary>
/// Summary of test
/// </Summary>
Public class test: ihttpmodule
{
Public test ()
{
//
// Todo: add the constructor logic here
//
}
# Region ihttpmodule Member
Public void dispose ()
{
// Throw new notimplementedexception ();
}
Public void Init (httpapplication context)
{
// Register the httpapplication acquirerequeststate event
Context. acquirerequeststate + = new eventhandler (context_acquirerequeststate );
}
Void context_acquirerequeststate (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
// Obtain the path of the current request
String Path = app. Context. Request. path;
// Determine whether the logon page is correct
If (! Path. endswith ("login. aspx "))
{// Determine whether the current logon user is admin
If (App. Context. User. Identity. Name! = "Admin ")
{// If the logged-on user is not admin, the current page cannot be viewed. redirect error page
App. Context. server. Transfer ("error. aspx? Message = "+ path + "");
}
}
// Throw new notimplementedexception ();
}
# Endregion
}
Http://xiaolongwang168.blog.163.com/blog/static/123642111201173095828628/