Use cookie and md5 encryption in asp.net to implement the Code for remembering the password Function

Source: Internet
Author: User

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

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.