Asp. NET Form Authentication Details _ practical skills

Source: Internet
Author: User

The authentication types for asp.net are as follows:

In our actual work, the froms authentication is still a lot of, we next detailed say:
As a web developer, I want to login to the form is too much contact. However, I found that some programmers in the authentication of the user name is stored in a session, and then into the system every page to verify that the session is empty, if not empty then Response.Redirect ("... aspx").
I think this approach is less than the form authentication provided by ASP.net, first of all, to increase the amount of code because we have to verify that the session exists on every page, and secondly, the session is stored in server memory, and I think that if you use it frequently, it will slow down the server. 。 and form authentication is different, it is to save the data in the cookie, so you can reduce the pressure on the server.
Example One:
Add two pages to the project: login.aspx (for login page) and main.aspx (main interface)
If we add the from authentication, then of course we first set up to not allow anonymous access to the site, and then we add the authenticated user to the cookie, the Web configuration file is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> 
<configuration> 
  <system.web> 
    < Compilation debug= "True" targetframework= "4.0"/> <authentication mode= 
 
   "Forms" > 
    <forms name= " Save "loginurl=" Login.aspx "protection=" All "> 
    </forms> 
   </authentication> 
 
   < authorization> 
    <deny users= "?" /> 
   </authorization> 
  </system.web> 
 
 

Description


After setting up, if we directly access the Main.aspx page, then we will jump to login.aspx.


We write the following code under the login button:

Using System; 
Using System.Collections.Generic; 
Using System.Linq; 
Using System.Web; 
Using System.Web.UI; 
Using System.Web.UI.WebControls; 
 
Namespace Lasttest 
{public 
  partial class Login:System.Web.UI.Page 
  { 
    protected void Page_Load ( Object sender, EventArgs e) 
    { 
 
    } 
 
    protected void Button1_Click (object sender, EventArgs e) 
    { 
      if (true)  You can query the database to verify that the user is legitimate 
      { 
        //Annotated two lines of the statement equivalent to the bottom of the statement is to save the user and then go back to the original page. 
        //system.web.security.formsauthentication.setauthcookie (TextBox1.Text, chkissavepwd.checked); 
        Response.Redirect ("main.aspx"); 
        System.Web.Security.FormsAuthentication.RedirectFromLoginPage (TextBox1.Text, chkissavepwd.checked); 
      } 
      else 
      { 
 
      } 
    } 
  } 

Of course, you can also delete authentication, exit login, we add a logout button on the main interface:


The code under logoff:

Using System; 
Using System.Collections.Generic; 
Using System.Linq; 
Using System.Web; 
Using System.Web.UI; 
Using System.Web.UI.WebControls; 
 
Namespace Lasttest 
{public 
  partial class Main:System.Web.UI.Page 
  { 
    protected void Page_Load (object sender, EventArgs e) 
    { 
 
    } 
 
    protected void Button1_Click (object sender, EventArgs e) 
    { 
      // Deletes from authentication ticket 
      System.Web.Security.FormsAuthentication.SignOut () from the browser; 
      Return to login page 
      Response.Redirect ("Login.aspx");}} 

Of course, if a system has a few people to use, then we can also add a fixed user, and then the user's password can be encrypted: if MD5 encryption or SHA1, of course, you can also use clear (plaintext, unsafe).

The above is about Asp.nett forms of authentication, I hope to help you learn.

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.