Asp.net under simulated using HttpModule Filter, to achieve access control

In asp.net, we have to prevent users from directly accessing the page specified Url to bypass login authentication, you need to add each page to verify or add validation template page. If the project is relatively large, add validation is a really big thing, this time, I just like to share a method to verify the use HttpModule achieve competence.

first step:

In the new project, a class .cs file, in this case, Filter, use of namespaces, mainly in order to access the Session.

using System.Web.SessionState;

Step 2: Ask the class inherits IHttpModel and IRequiresSessionState interface code is as follows:

public class Filter : IHttpModule, IRequiresSessionState
{
}

The third step: two methods to achieve Ihttpmodel interface look:

Copy the code
public void Dispose()
{
            
}
public void Init(HttpApplication context) { }
Copy the code

The fourth step: the preparation method context_AcquireRequestState, achieve certification authority, as follows:

Copy the code
void context_AcquireRequestState(object sender, EventArgs e)
        {

            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            
            try { string path = context.Request.Path; if (context.Session != null && context.Session["User"] == null ) { context.Response.Redirect("~/Default.aspx"); } } catch (Exception) { context.Response.Write("系统出现错误,请稍后访问......"); } }
Copy the code

Step Five: Sign AcquireRequestState method:

public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

So far, this privilege authentication is completed friends ~ ~

Note: You can not use this method BeginRequest, BeginRequest methods can not access Session, errors occur. Specifically why, also please explain experts.

HttpModel events and perform the following sequence:

        AcquireRequestState  ready to receive the current status dialogue HTTP request when ASP.NET runtime when triggered this event.
        AuthenticateRequest  ready to authenticate users when ASP.NET runtime when triggered this event.
        AuthorizeRequest  ready to authorized users from accessing resources when ASP.NET runtime when triggered this event.
        BeginRequest  reception when ASP.NET runtime when a new HTTP request triggered this event.
        Disposed  This event is raised when a complete ASP.NET HTTP request processing.
        EndRequest  the contents of the response before sending a client to trigger this event.
        Error  when unhandled exception occurs during the processing of HTTP request triggered this event.
        PostRequestHandlerExecute  This event is raised when the end of the implementation of the HTTP handler.
        PreRequestHandlerExecute  This event is raised before the start of the implementation of the ASP.NET HTTP request processing program. After this event, ASP.NET forwards the request to the appropriate HTTP handler.
        PreSendRequestContent  in ASP.NET content before sending the response to the client to trigger this event. This event allows us to change the response content before the content reaches the client. We can use this event to add to the page output for the contents of all pages. For example generic menu, header information or feet.
        PreSendRequestHeaders This event is raised before sending HTTP response header information to the client in ASP.NET. Before header information reaches the client, this event allows us to change its contents. We can use this event to add custom data in the cookie and header information.
        ReleaseRequestState  when the end of the search, some ASP.NET request handler executed when triggered this event.
        ResolveRequestCache  we caused this event to determine whether you can use the content returned from the output buffer to the end of the request. How to set the time depending on the output buffer of the Web application.
        UpdateRequestCache  when finished with the current ASP.NET HTTP request processing, and output content when ready to add to the output buffer, triggering this event. Depending on how the Web application output buffer is set.

REFERENCE FORM : http://www.cnblogs.com/yunfeifei/p/3699937.html

Reproduced in: https: //www.cnblogs.com/zhangchenliang/p/4108418.html

Guess you like

Origin blog.csdn.net/weixin_34396103/article/details/93495466