一、登陆时记住我功能实现。
1. 首先在前端界面添加一个单选框,当表单提交的时候会一起提交过来。同时单选框发送的属性值分为ture和false。
<input type="submit" id="btnLogin" value="登录" class="login-btn" /><span id="errorMsg" style="font-size:14px;color:red;display:none"></span> <input type="checkbox" value="1" name="checkbox" />记住我
2. 如果选中,也就是返回为ture,执行如下
//如果选中记住我,那么将值存进cookie中 if (!string.IsNullOrEmpty(Request["checkbox"])) { HttpCookie cookie1 = new HttpCookie("cp1", userInfo.UName); HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.GetMd5String(userInfo.UPwd)); cookie1.Expires = DateTime.Now.AddDays(2); cookie2.Expires = DateTime.Now.AddDays(2); Response.Cookies.Add(cookie1); Response.Cookies.Add(cookie2); }
3. 当退出登陆时,清空cookie中的值
//退出登陆 #region 退出登陆 [HttpPost] public ActionResult Quit() { Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1); Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1); if (Request.Cookies["sessionId"] != null) { //首先获取到Memcache对应的cookies值。 string sessionId = Request.Cookies["sessionId"].Value; object obj = Common.MemcacheHelper.Get(sessionId); if (obj != null) { //Response.Cookies["sessionId"].Expires = DateTime.Now.AddDays(-1); //UserInfo userinfo = obj as UserInfo; UserInfo userinfo = Common.SerializeHelper.DeserializeToObject<UserInfo>(obj.ToString()); Loginuser = userinfo; Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddDays(-1));//模拟滑动过期时间 Common.MemcacheHelper.Delete(sessionId); } } //string str=Request.Cookies["sessionId"].Value; //Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20)) //Common.MemcacheHelper.Delete(str); //Response.Cookies["sessionId"].Expires = DateTime.Now.AddDays(-1); //Request.Cookies.Clear(); return Content("1"); //return RedirectToAction("/Login/Index"); } #endregion