1.//读取保存的Cookie信息
HttpCookie cookies = Request.Cookies["USER_COOKIE"];
if (cookies != null)
{
//如果Cookie不为空,则将Cookie里面的用户名和密码读取出来赋值给前台的文本框。
this.txtUserName.Value = cookies["UserName"];
this.txtPassword.Attributes["value"] = cookies["UserPassword"];
}
2.把用户名和密码写入Cookie
HttpCookie cookie = new HttpCookie("USER_COOKIE");
if (this.chkIsRemember.Checked) //记住密码勾选
{
//所有的验证信息检测之后,如果用户选择的记住密码,则将用户名和密码写入Cookie里面保存起来。
cookie.Values.Add("UserName", this.txtUserName.Value.Trim());
cookie.Values.Add("UserPassword", this.txtPassword.Value.Trim());
//这里是设置Cookie的过期时间,这里设置一个星期的时间,过了一个星期之后状态保持自动清空。
cookie.Expires = System.DateTime.Now.AddDays(7.0);
HttpContext.Current.Response.Cookies.Add(cookie);
}
else
{
if (cookie["USER_COOKIE"] != null)
{
//如果用户没有选择记住密码,那么立即将Cookie里面的信息情况,并且设置状态保持立即过期。
Response.Cookies["USER_COOKIE"].Expires = DateTime.Now;
}
}