Asp.net uses cookies to automatically log on with the Remember password

Source: Internet
Author: User

This article describes how asp.net uses cookies to complete the code for remembering the automatic Password Logon function. It uses cookies to remember the user's logon information stored on the client. The next time it comes in, it will be KO.

// ------------------------------ Logon page ----------------------------

The Code is as follows: Copy code


String username = this.txt UserName. Text; // User Name
String password = this.txt Password. Text; // password

If (UserManager. LoginValidate (username, password) & this. ckState. Checked = true) // check whether the logon name and password are correct and whether the check box for remembering the username and password is selected
{
// Clear the Cookie before determining whether the client browser exists.

If (Request. Cookies ["username"]! = Null & Request. Cookies ["password"]! = Null)
{
Response. Cookies ["username"]. Expires = System. DateTime. Now. AddSeconds (-1); // Expires expiration time
Response. Cookies ["password"]. Expires = System. DateTime. Now. AddSeconds (-1 );
}
Else
{
// Add a Cookie to the client browser (it is best to use MD5 encryption for the user name and password)
HttpCookie hcUserName1 = new HttpCookie ("username ");
HcUserName1.Expires = System. DateTime. Now. AddDays (7 );
HcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie ("password ");
HcPassword1.Expires = System. DateTime. Now. AddDays (7 );
HcPassword1.Value = password;
Response. Cookies. Add (hcUserName1 );
Response. Cookies. Add (hcPassword1 );
}
}



// ------------------------ Go to the logon page (preferably with a motherboard page )------------------------

The Code is as follows: Copy code
If (Request. Cookies ["username"]! = Null & Request. Cookies ["password"]! = Null)
{
// The user has logged on

Username = Request. Cookies ["username"]. Value. ToString (); // read Cookie
Password = Request. Cookies ["password"]. Value. ToString (); // determine whether the user name and password read by the Cookie can be correctly logged on
If (UserManager. LoginValidate (username, password ))
{
// Logon code
}}

// ------------------------------- Exit Cookie (add an exit button )-------------------------------

// Exit

The Code is as follows: Copy code
HttpCookie hcUserName1 = new HttpCookie ("username ");
HcUserName1.Expires = System. DateTime. Now. AddDays (-7 );
HcUserName1.Value = username;
HttpCookie hcPassword1 = new HttpCookie ("password ");
HcPassword1.Expires = System. DateTime. Now. AddDays (-7 );
HcPassword1.Value = password;
Response. Cookies. Add (hcUserName1 );
Response. Cookies. Add (hcPassword1 );

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.