asp.net Cookie 用户登陆时记住我
/// <summary> /// 判断Cookie中存储的数据 /// </summary> protected void CheckUserCookie() { //先判断Cookie是否有值 if (Request.Cookies["cp1"] != null && Request.Cookies["cp2"] != null) { //校验数据是否正确. string cookieUserName = Request.Cookies["cp1"].Value; string cookieUserPwd=Request.Cookies["cp2"].Value; BLL.UserManager bll = new BLL.UserManager(); Model.User model=bll.GetModel(cookieUserName);//根据Cookie中存储的用户名找用户 if (model != null) { //判断密码是否正确. //如果注册时,采用相同的加密方式,那么这里再比较时直接比较 if (Enctry(model.LoginPwd) == cookieUserPwd) { Session["UserInfo"] = model; GoPage("登录成功"); } else//表示密码错误,删除Cookie { Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); } } } } /// <summary> /// 校验用户登录信息 /// </summary> protected void CheckUserLogin() { string txtName=Request.Form["txtName"]; string txtPwd=Request.Form["txtPwd"]; BLL.UserManager bll = new BLL.UserManager(); string msg = string.Empty; Model.User model = null; //校验用户名密码 bool b= bll.UserLogin(txtName, txtPwd,out msg,out model); if (b) { Session["UserInfo"] = model; //如果选择了记住我复选框,将用户的信息写到Cookie。 if (!string.IsNullOrEmpty(Request.Form["checkMe"])) { HttpCookie cookie1 = new HttpCookie("cp1", model.LoginId); HttpCookie cookie2 = new HttpCookie("cp2",Enctry(model.LoginPwd)); cookie1.Expires = DateTime.Now.AddDays(3); cookie2.Expires = DateTime.Now.AddDays(3); Response.Cookies.Add(cookie1); Response.Cookies.Add(cookie2); } GoPage(msg); } else { Response.Redirect("/ShowMsg.aspx?msg=" + Server.UrlEncode(msg) + "&txt=" + Server.UrlEncode("登录页") + "&url=/Login.aspx"); } }