在做一個(gè)前臺(tái)的登陸和后臺(tái)的信息審核管理功能時(shí),需要用到記住密碼的模塊:
雖然.net內(nèi)置了登陸控件,有記住密碼的功能,但還是想自己實(shí)踐一下,以下代碼主要應(yīng)用了COOKIE,包括安全加密的過(guò)程等。
復(fù)制代碼 代碼如下:
//設(shè)置,刪除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";
//保存用戶名和密碼到cookie
if (UserNameCookie == null)
{
UserNameCookie = new HttpCookie("UserNameCookie");
UserNameCookie.Values.Add("UserName", TxtUserName.Text);
UserNameCookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(UserNameCookie);
}
//修改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"); //如果重新指定用戶密碼,則重新加密密碼
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”是程現(xiàn)在密碼框中的十個(gè)字符。
{
SetToCookie(UserPasswordCookie, "UserPassword", FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"));
}
}
else
{
lblcookie.Text = "0";
//從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);
}
}
}
//檢查是否存在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");//設(shè)置密碼框初始值
}
}
public string getpassword()
{
HttpCookie UserPasswordCookie = Request.Cookies["UserPasswordCookie"];
string strpwd = ""; //獲取此密碼字符串與數(shù)據(jù)庫(kù)存中的密布相比。
if (lblcookie.Text == "1")
{
strpwd = UserPasswordCookie.Values["UserPassWord"];//直接獲取COOKIE中的密碼值
}
else
{
strpwd= FormsAuthentication.HashPasswordForStoringInConfigFile(TxtUserPassword.Text, "MD5"); //對(duì)密碼進(jìn)行加密
}
return strpwd; //返回密碼字段
}
//修改COOKIE這段的方法
public void SetToCookie(HttpCookie httpcookie, string cookiename, string cookievalue)
{
httpcookie.Values[cookiename] = cookievalue;
httpcookie.Expires = DateTime.Now.AddDays(30);
Response.Cookies.Add(httpcookie);
}
}
您可能感興趣的文章:- ASP.NET中DES加密與解密MD5加密幫助類的實(shí)現(xiàn)代碼
- VB.NET實(shí)現(xiàn)的MD5加密算法示例【32位】
- .NET MD5加密解密代碼解析
- asp.net實(shí)現(xiàn)的MD5加密和DES加解密算法類完整示例
- asp.net實(shí)現(xiàn)md5加密
- vb 中的MD5加密在asp.net中的實(shí)現(xiàn)
- ASP.NET中MD5與SHA1加密的幾種方法
- 徹底解決ASP.NET MD5加密中文結(jié)果和ASP不一致的問(wèn)題
- asp.net下常用的加密算法MD5、SHA-1應(yīng)用代碼
- asp.net中MD5 16位和32位加密函數(shù)
- ASP.net中md5加密碼的方法
- .net core使用MD5加密解密字符串