Asp.net authentication mechanism instance code

Source: Internet
Author: User

ASP. NET provides three authentication methods: windows authentication, Forms authentication, and Passport authentication.
Windows Authentication: IIS performs Authentication Based on application settings. To use this authentication method, you must disable anonymous access in IIS.
Forms authentication: use cookies to save user creden。 and redirect unauthenticated users to the custom logon page.
Passport authentication: this service is executed by Microsoft's centralized Identity Authentication Service. It provides independent logon and core configuration file services for member sites.

About the configuration of these three authentication methods, we recommend an article: http://www.jb51.net/article/30510.htm

Here I will explain the Forms authentication method in detail, the practical application and deep understanding in ASP. NET projects.

The Application of ASP. NET authentication mechanism in project development will make the project easily and quickly implement page access permissions, and save unnecessary security issues.
The validation mechanism provided by ASP. NET can be used flexibly only after deep understanding. For the access permissions of the entire project, sometimes we need to restrict the access permissions of some pages without affecting access to other public pages. Here is an example of a project.
Case: There is an ASP. NET project that restricts access to the entire project. Anonymous users are not allowed to access the project, except for Logon pages.
The problem is that a file of the Script applied in the Login page (assuming it is the Login. aspx file under the Manager directory) (assuming this file is: Script/jquery-1.7.1.min.js)

1. web. config configuration fileCopy codeThe Code is as follows: <! -- Configure Forms identity authentication and do not allow anonymous users to access it; otherwise, the/Manager/login. aspx page is displayed. -->
<Authentication mode = "Forms">
<Forms name = "myTeacher" loginUrl = "~ /Manager/login. aspx "protection =" All "path ="/"timeout =" 60 "> </forms>
</Authentication>
<Authorization>
<Deny users = "? "/>
</Authorization>

Now we can understand that when the user opens the program, it will automatically go to the Manager/login. aspx page, but the jquery-1.7.1.min.js file referenced by this page is not available. Because it restricts all anonymous users to access the Login. aspx page only. Other pages or files can be accessed normally only after the ticket is registered. So when anonymous users need to have access to jquery-1.7.1.min.js files, they need to set access to a file or file to all users.Copy codeThe Code is as follows: <! -- Allow all users to access the Script folder -->
<Location path = "Script">
<System. web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. web>
</Location>

Path can be just a specific file, such as "Script/jquery-1.7.1.min.js", where the "Script" folder is set to indicate that anonymous users are allowed to access the files under the "Script" folderCopy codeThe Code is as follows: <! --
Define ASP. NET authentication mechanism
-->
<! -- Allow all users to access the Script folder -->
<Location path = "Script">
<System. web>
<Authorization>
<Allow users = "*"/>
</Authorization>
</System. web>
</Location>

<System. web>
<CustomErrors mode = "Off" defaultRedirect = ".../error. aspx"/>

<! -- Configure Forms identity authentication and do not allow anonymous users to access it; otherwise, the/Manager/login. aspx page is displayed. -->
<Authentication mode = "Forms">
<Forms name = "myTeacher" loginUrl = "~ /Manager/login. aspx "protection =" All "path ="/"timeout =" 60 "> </forms>
</Authentication>
<Authorization>
<Deny users = "? "/>
</Authorization>

2. log on to the registration ticket

After the logon page is submitted and successfully logged on, register the ticket. After the ticket is registered, the identity of the current user is no longer an anonymous user and the access permission is not limited. The namespace of FormsAuthentication is: using System. Web. Security;

FormsAuthentication. RedirectFromLoginPage (UserID, false); // key UsersID for page registration indicates the login ID
3. Tear up the ticket (exit the system)

FormsAuthentication. SignOut (); // tore the ticket and log out.

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.