When performing a front-end login and background information review management function, you need to use the password-remembering module:
Although. net has built-in login controls and has the ability to remember passwords, I still want to practice it myself. The following code mainly applies cookies, including the secure encryption process.
Copy codeThe Code is as follows:
// Set and delete the Cookie
// Provider jb51.net
Protected void set_cookie ()
{
HttpCookie UserNameCookie = Request. Cookies ["UserNameCookie"];
HttpCookie UserPasswordCookie = Request. Cookies ["UserPasswordCookie"];
If (this. CheSave. Checked)
{
Lblcookie. Text = "1 ";
// Save the user name and password to the cookie
If (UserNameCookie = null)
{
UserNameCookie = new HttpCookie ("UserNameCookie ");
UserNameCookie. Values. Add ("UserName", TxtUserName. Text );
UserNameCookie. Expires = DateTime. Now. AddDays (30 );
Response. Cookies. Add (UserNameCookie );
}
// Modify the COOKIE
Else if (UserNameCookie. Values ["UserName"]! = TxtUserName. Text)
{
SetToCookie (UserNameCookie, "UserName", TxtUserName. Text );
}
If (UserPasswordCookie = null)
{
UserPasswordCookie = new HttpCookie ("UserPasswordCookie ");
String password1 = FormsAuthentication. HashPasswordForStoringInConfigFile (TxtUserPassword. Text, "MD5"); // If you specify a new user password, re-encrypt the password
UserPasswordCookie. Values. Add ("UserPassword", password1 );
UserPasswordCookie. Expires = DateTime. Now. AddDays (30 );
Response. Cookies. Add (UserPasswordCookie );
}
Else if (UserPasswordCookie. Values ["UserPassword"]! = FormsAuthentication. HashPasswordForStoringInConfigFile (TxtUserPassword. Text, "MD5") & TxtUserPassword. Text! = "1234567890 ")
// "1234567890" is ten characters in the current password box.
{
SetToCookie (UserPasswordCookie, "UserPassword", FormsAuthentication. HashPasswordForStoringInConfigFile (TxtUserPassword. Text, "MD5 "));
}
}
Else
{
Lblcookie. Text = "0 ";
// Delete the user name and password from the cookie
If (Response. Cookies ["UserNameCookie"]! = Null)
{
HttpCookie myCookie = new HttpCookie ("UserNameCookie ");
MyCookie. Expires = DateTime. Now. AddDays (-1d );
Response. Cookies. Add (myCookie );
}
If (Response. Cookies ["UserPasswordCookie"]! = Null)
{
HttpCookie myCookie = new HttpCookie ("UserPasswordCookie ");
MyCookie. Expires = DateTime. Now. AddDays (-1d );
Response. Cookies. Add (myCookie );
}
}
}
// Check whether there is a COOKie
Public void check_cookie ()
{
HttpCookie UserNameCookie = Request. Cookies ["UserNameCookie"];
HttpCookie UserPasswordCookie = Request. Cookies ["UserPasswordCookie"];
If (UserNameCookie! = Null)
{
This. CheSave. Checked = true;
TxtUserName. Text = UserNameCookie. Values ["UserName"];
}
If (UserPasswordCookie! = Null)
{
TxtUserPassword. Attributes. Add ("value", "1234567890"); // you can specify the initial value of the password.
}
}
Public string getpassword ()
{
HttpCookie UserPasswordCookie = Request. Cookies ["UserPasswordCookie"];
String strpwd = ""; // obtain this password. The string is compared to the dense data in the data inventory.
If (lblcookie. Text = "1 ")
{
Strpwd = UserPasswordCookie. Values ["UserPassWord"]; // directly obtain the password value in the COOKIE
}
Else
{
Strpwd = FormsAuthentication. HashPasswordForStoringInConfigFile (TxtUserPassword. Text, "MD5"); // encrypt the password
}
Return strpwd; // return the password field.
}
// Modify the COOKIE
Public void SetToCookie (HttpCookie httpcookie, string cookiename, string cookievalue)
{
Httpcookie. Values [cookiename] = cookievalue;
Httpcookie. Expires = DateTime. Now. AddDays (30 );
Response. Cookies. Add (httpcookie );
}
}