asp.net中使用cookie与md5加密实现记住密码功能的实
网络编程 2021-07-04 22:40www.168986.cn编程入门
虽然.内置了登陆控件,有记住密码的功能,但还是想自己实践一下,以下代码主要应用了COOKIE,包括安全加密的过程等
在做一个前台的登陆和后台的信息审核管理功能时,需要用到记住密码的模块
虽然.内置了登陆控件,有记住密码的功能,但还是想自己实践一下,以下代码主要应用了COOKIE,包括安全加密的过程等。
代码如下:
//设置,删除Cookie
//provider jb51.
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);
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程