When creating a website, you often need to verify that the user has logged on to each page. If you have not logged on to the system, you will be redirected to the logon page. Where is the verification process? The most common method for beginners isEach pageThe following code verifies the user in the session in the Page_Load event:
If you do this, the above Code will appear on all the pages on the site that require authentication. It is not a good solution to copy the code so many times. Here we use the other two methods to solve this problem.
? Method 1: Use a Global File
? Method 2: Use the Form provided by. Net for verification.
The two methods are very easy to implement the above functions, especially the Form authentication provided by. Net is flexible and can be basically solved for various permission issues. The following is a brief description:
1. Method 1: Global File
It is easy to think that because Global is a Global file, every request will first be processed by this file, so here the authentication can replace the authentication on all pages. The events in the Global File and their functions are not mentioned here.
Add the Global application class (Global. asax) ". If you use Session to authenticate your identity, you will be directed to the Session_Start event (originally intended to be written in Application_BeginRequest or Application_AuthenticateRequest, but the Application is a global object applied to the entire Application, the session is unique to each user, so you just need to add the authentication code. For example:
Void Session_Start (object sender, EventArgs e) {// code run when the new Session starts if (Session ["user"] = null) {Response. redirect ("Login. aspx ");}}
In this way, the same authentication function is implemented on each page. (Note: The above method of session authentication does not allow multiple users to log on to the same browser)
2. Method 2: Form Verification provided by. Net
2.1 First, open the web. config file and add the following code in the system. web Section:
This solves the problem above. The following describes the configuration principles of web. config verified by Form. For example, the directory structure of my project is as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + expires/z8K1xNKzw + a2vLHY0Ou + expires/expires + 69/LXEuLjEv8K8wO + expires + 9raoo6k8L3A + expires "brush: java; ">
(2) directly modify the original configuration file and add a location section under the configuration section. The following is the modified configuration file:
To enable the Default. aspx page in the root directory without authentication, set the path attribute of the preceding location to "Default. aspx.
3. Summary
Form Verification provided by. NET uses a specific cookie to verify the existence of the cookie upon each request to the server, so as to determine whether the user has the relevant permissions. This method is convenient to use and is much safer than session verification. It can also implement role-based permission Control for different directories, which is flexible to use, find the required information online.