ASP. NET form authentication details, asp.net form Authentication

Source: Internet
Author: User
Tags server memory

ASP. NET form authentication details, asp.net form Authentication

The authentication type of asp.net is as follows:

 

In our actual work, froms still uses a lot of authentication, so let's talk about it in detail:
As a web development programmer, I think there are too many contacts to log on to the form. However, I found that some programmers stored the authenticated user name in a session during authentication, and then went to every page of the system to verify whether the session was empty, response. redirect ("...... aspx ").
I think this method is insufficient for form authentication provided by asp.net. The first step is to increase the amount of code, because we need to verify whether the session exists on every page. Second, sessions are stored in the server memory. I think that if you use sessions frequently, the server will be slowed down. Form authentication is different. It stores data in cookies, so it can reduce the pressure on the server.
Example 1:
Add two pages to the project: login. aspx (used for Logon) and main. aspx (main interface)
If we have added from authentication, we must first set not to allow anonymous access to the website, and then add the authenticated users 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>   </configuration> 

Note:


After setting, If we directly access the main. aspx page, the page will jump to login. aspx.


Write the following code under the logon 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 button#click (object sender, EventArgs e) {if (true) // You can query the database to verify whether the user is legal {// the two statements commented out are equivalent to the following statement: Save the user and return 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 and log out. We will add a Logout button on the main interface:


Code for logout:

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 button#click (object sender, EventArgs e) {// Delete the from authentication ticket System from the browser. web. security. formsAuthentication. signOut (); // return to the logon page Response. redirect ("login. aspx ");}}}

Of course, if there are several users in a system, we can also add a fixed user, and then encrypt the user's password: If MD5 encryption or SHA1, you can also use clear (plaintext, insecure ).

The above is about the form authentication of ASP. NETt, and I hope it will help you learn it.

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.