Implementation of ASP. NET form Authentication

Source: Internet
Author: User

Asp.net form-based authentication prevents users from skipping logon and directly entering URLs to access the page. Unlike Session control, each page has to write code.

ASP. NET form authentication step 1: Configure web. config (web. config must be case sensitive. Generally, the key is in the camelCasing style and the value is in the PascalCasing style)

 
 
  1. <System. web>
  2. <Authentication mode ="Forms">
  3. <Forms name ="MainForm"LoginUrl ="Frame/Login. aspx"Protection ="All"Timeout ="60"/>
  4. </Authentication>
  5. <Authorization>
  6. <Deny users ="? "/> <! -- Do not write <allow users ="*"/> Otherwise, the logon page is not displayed during anonymous access -->
  7. </Authorization>
  8. </System. web>

Step 2 of ASP. NET form authentication: After the user name and password are verified in the logon button event,

 
 
  1. // Record Cookie 
  2. FormsAuthentication. SetAuthCookie (loginName,False);
  3.  
  4. // Redirect page 
  5. Response. Redirect ("Frame.htm");
  6.  

If you want to redirect to the page you want to access, that is, the page accessed before the logon page, you can use the following method to obtain the URL of the page accessed by the user at the beginning:

 
 
  1. String redirectionUrl = FormsAuthentication.GetRedirectUrl(loginName,false); 

Then, determine whether the obtained URL is a logon page or an index page. If yes, redirectionUrl is assigned a new value to the page URL accessed during normal logon.

 
 
  1. if(redirectionUrl.ToLower().IndexOf("login.aspx") > 0)  
  2.                   redirectionUrl = "Frame.htm";  

ASP. NET form authentication completed: the final redirection page is ready.

 
 
  1. Response.Redirect(redirectionUrl); 
  1. ASP. NET1.1, ASP. NET2.0, ASP. NET3.5, etc.
  2. ASP. NET1.1 and ASP. NET2.0: different settings of database connection strings
  3. ASP. NET1.1 and ASP. NET2.0 coexist
  4. ASP. NET1.1 implements the MasterPage function of imitation 2.0
  5. Experience in upgrading ASP. NET1.1 to ASP. NET2.0

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.