URL filtering implementation code in ASP. NET

Source: Internet
Author: User

The following is the definition of a class.
Copy codeThe Code is as follows:
Using System;
Using System. Web;
Using System. Web. SessionState;

Namespace QTJZ
{
Public class Filters: IHttpModule, IRequiresSessionState
{
Public void Dispose (){}

Public void Init (HttpApplication application)
{
Application. AcquireRequestState + = new EventHandler (application_AcquireRequestState );
}

Public void application_AcquireRequestState (object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpRequest request = application. Request;
HttpResponse response = application. Response;

String url = request. CurrentExecutionFilePath. Trim ('/');
String suffix = request. CurrentExecutionFilePathExtension. Trim ('.');

If (! Url. Equals ("Default.htm") & (suffix = "aspx" | suffix = "htm "))
{
Object sessionObj = application. Context. Session = null? Null: application. Session ["useID"];
If (sessionObj = null)
{
Response. Redirect ("~ /Default.htm ");
}
}
}
}
}

To implement the filtering effect, the Filters class needs to implement the IHttpMoeld interface, which requires two methods: Dispose and Init. The Init parameter is an HttpApplication instance that registers events. Because the URL is to be filtered, the registration is an AcquireRequestState event. Similar events are listed as follows:

BeginRequest When ASP. NET responds to a request, it occurs as the first event in the HTTP execution pipeline chain.
AuthenticateRequest This occurs when a user ID has been created for the security module.
AuthorizeRequest This occurs when the security module has verified user authorization.
ResolveRequestCache

Authorization events are completed in ASP. NET to enable the cache module to provide services for requests from the cache,

This bypasses the execution of Event Handlers (such as a page or XML Web services.

AcquireRequestState This occurs when ASP. NET acquires the current State (such as session state) associated with the current request.
PreRequestHandlerExecute Occurs just before ASP. NET starts executing the event processing program (for example, a page or an XML Web Service.
PostRequestHandlerExecute Occurs when an ASP. NET event handler (for example, a page or an XML Web service) is executed.
ReleaseRequestState After ASP. NET executes all request event handlers. This event causes the status module to save the current status data.
UpdateRequestCache When ASP. NET executes the event handler so that the cache module storage will be used to provide a response for subsequent requests from the cache.
EndRequest When ASP. NET responds to a request, it occurs as the last event in the HTTP execution pipeline chain.

You can use the CurrentExecutionFilePath attribute of the request to obtain the url to jump to. You can use the CurrentExecutionFilePathExtension attribute to obtain the suffix of the request file. You can determine the rules based on your needs. In this case, I will determine whether the Session exists during the request. If the Session does not exist, I will jump back to the login page. Because Session is used, an exception is thrown when the page is opened. The exception message is "Session status is unavailable in this context .", No exception is thrown after the IRequiresSessionState interface is implemented.

In addition, add the following code under the <system. Web> node of the configuration file web. config:
Copy codeThe Code is as follows:
<HttpModules>
<Add name = "filters" type = "QTJZ. Filters"/>
</HttpModules>

The type attribute is the fully qualified name of the above Filters class.

Related Article

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.