asp下使用Cookie保存登录信息
asp下使⽤Cookie保存登录信息
在⽹页中登录窗⼝是最常见的,如果把登录信息存在客户机Cookie中,下次⽤户登录时,⽹页先在客户机上查登录信息,如果成功即可跳过登录步骤直接到主窗⼝,如登录界⾯如下:
实现:
1、第⼀次登录时保存到Cookie中,我使⽤的是ActionResult:
public ActionResult Login(User user)
{
配搭衣服if (string.IsNullOrEmpty(user.Name) || string.IsNullOrEmpty(user.Pwd))
{
return Content("no:请输⼊完整,不能为空!");
}
User dbuser = dao.GetUserByName(user);
if (dbuser == null)
{
return Content("no:⽤户名错误!");
粉蒸排骨的做法}
if (dbuser.Pwd != user.Pwd)
{
return Content("no:密码错误!");
}
var record = Request["ckbrecord"].ToBoolean();
SetCookie(record,user);
描写冬至的优美诗句古诗Session["userinfo"] = dbuser;
return Content("ok:"+dbuser.Rank);
}
//设置cookie;
private void SetCookie(bool record, User user)
西游记之三打白骨精票房{
if (record)
{
HttpCookie cookiename = new HttpCookie("printName", DES.MD5Encrypt(HttpUtility.UrlEncode(user.Name)));
cookiename.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookiename);
HttpCookie cookiepwd = new HttpCookie("printPwd", DES.MD5Encrypt(HttpUtility.UrlEncode(user.Pwd)));
cookiepwd.Expires = DateTime.MaxValue;
Response.Cookies.Add(cookiepwd);
}
else
{
HttpCookie cookiepwd = Request.Cookies["printPwd"];
if (cookiepwd != null)
{
cookiepwd.Expires = DateTime.Now.AddHours(-1);
Response.Cookies.Add(cookiepwd);
}
}学唱歌如何练声
}
2、登录界⾯前检查Cookie,把下⾯判断放在⾸页Action中
//cookie登录
private User CookieLogin()
{
HttpCookie printName = Request.Cookies["printName"];
HttpCookie printPwd = Request.Cookies["printPwd"];
User user = new User();
心居施源结局if (printName != null && printPwd != null)
{
user.Name =HttpUtility.UrlDecode(DES.MD5Decrypt(printName.Value));
user.Pwd = HttpUtility.UrlDecode(DES.MD5Decrypt(printPwd.Value));
}
return user;
}
HttpUtility.UrlDecode和HttpUtility.UrlEncode是处理中⽂乱码问题⽤的。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。