記住我功能實現

1、登錄時記住我功能實現。前端

1. 首先在前端界面添加一個單選框,當表單提交的時候會一塊兒提交過來。同時單選框發送的屬性值分爲ture和false。cookie

<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,執行以下session

 //若是選中記住我,那麼將值存進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中的值ui

  //退出登錄
        #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
相關文章
相關標籤/搜索