asp.net using cookies and MD5 encryption implementation code to remember the function of the password _ practical skills

Source: Internet
Author: User

In doing a front landing and backstage information audit management function, need to use the module to remember the password:
Although. NET built in login control, there is the ability to remember the password, but still want to practice, the following code mainly applies cookies, including the process of security encryption.

Copy Code code as follows:

settings, deleting cookies
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 user name and password to cookie
if (Usernamecookie = null)
{
Usernamecookie = new HttpCookie ("Usernamecookie");
USERNAMECOOKIE.VALUES.ADD ("UserName", txtUsername.Text);
Usernamecookie.expires = DateTime.Now.AddDays (30);
RESPONSE.COOKIES.ADD (Usernamecookie);
}
modifying cookies
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"); Re-encrypt password if user password is re specified
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 the 10 characters in the Process now password box.
{
Settocookie (Userpasswordcookie, "UserPassword", FormsAuthentication.HashPasswordForStoringInConfigFile ( Txtuserpassword.text, "MD5"));
}
}
Else
{
Lblcookie. Text = "0";
Remove user names and passwords from cookies
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 for cookie status
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");//Set Password box initial value
}
}
public string GetPassword ()
{
HttpCookie Userpasswordcookie = request.cookies["Userpasswordcookie"];
String strpwd = ""; Gets this password string compared to the clouds in the database store.
if (Lblcookie. Text = = "1")
{
Strpwd = userpasswordcookie.values["UserPassword"];//get the password value directly from the cookie
}
Else
{
Strpwd= FormsAuthentication.HashPasswordForStoringInConfigFile (Txtuserpassword.text, "MD5"); Encrypt a password
}
return strpwd; return password field
}
Modify cookie This section of the method
public void Settocookie (HttpCookie HttpCookie, String cookiename, String cookievalue)
{
HttpCookie. Values[cookiename] = Cookievalue;
HttpCookie. Expires = DateTime.Now.AddDays (30);
RESPONSE.COOKIES.ADD (HttpCookie);
}
}

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.