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 );
|