asp.net中使用cookie与md5加密实现记住密码功能的实现代码
程序员文章站
2024-03-04 21:14:18
在做一个前台的登陆和后台的信息审核管理功能时,需要用到记住密码的模块:虽然.net内置了登陆控件,有记住密码的功能,但还是想自己实践一下,以下代码主要应用了cookie,包...
在做一个前台的登陆和后台的信息审核管理功能时,需要用到记住密码的模块:
虽然.net内置了登陆控件,有记住密码的功能,但还是想自己实践一下,以下代码主要应用了cookie,包括安全加密的过程等。
复制代码 代码如下:
//设置,删除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”是程现在密码框中的十个字符。
{
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");//设置密码框初始值
}
}
public string getpassword()
{
httpcookie userpasswordcookie = request.cookies["userpasswordcookie"];
string strpwd = ""; //获取此密码字符串与数据库存中的密布相比。
if (lblcookie.text == "1")
{
strpwd = userpasswordcookie.values["userpassword"];//直接获取cookie中的密码值
}
else
{
strpwd= formsauthentication.hashpasswordforstoringinconfigfile(txtuserpassword.text, "md5"); //对密码进行加密
}
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);
}
}
上一篇: IO流常用类的总结