记住登录用户,下次登录

<td align="center" colspan="2">
                                    <input type="submit" id="btnLogin" value="登录" class="login-btn" /><input type="checkbox" name="checkMe" value="1"/>记住我<span id="errorMsg" style="font-size:14px;color:red"></span>
                                </td>

 

//记住我
               if (!string.IsNullOrEmpty(Request["checkMe"]))
               {
                   HttpCookie cookie1 = new HttpCookie("cp1", userInfo.UName);
                   HttpCookie cookie2 = new HttpCookie("cp2", Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userInfo.UPwd)));
                   cookie1.Expires = DateTime.Now.AddDays(3);
                   cookie2.Expires = DateTime.Now.AddDays(3);
                   Response.Cookies.Add(cookie1);
                   Response.Cookies.Add(cookie2);
               }

 

public ActionResult Index()
        {
            CheckCookieInfo();
            return View();
        }

----------------------------------------------------------

        #region 判断Cookie信息
          private void CheckCookieInfo()
          {
              if (Request.Cookies["cp1"] != null && Request.Cookies["cp2"] != null)
              {
                  string userName = Request.Cookies["cp1"].Value;
                  string userPwd = Request.Cookies["cp2"].Value;
                  //判断Cookie中存储的用户密码和用户名是否正确.
                 var userInfo=UserInfoService.LoadEntities(u=>u.UName==userName).FirstOrDefault();
                  
                 if (userInfo != null)
                 {
                     //注意:将用户的密码保存到数据库时一定要加密。
                     //由于数据库中存储的密码是明文,所以这里需要将数据库中存储的密码两次MD5运算以后在进行比较。
                     if (Common.WebCommon.GetMd5String(Common.WebCommon.GetMd5String(userInfo.UPwd)) == userPwd)
                     {
                         string sessionId = Guid.NewGuid().ToString();//作为Memcache的key
                         Common.MemcacheHelper.Set(sessionId, Common.SerializerHelper.SerializeToString(userInfo), DateTime.Now.AddMinutes(20));//使用Memcache代替Session解决数据在不同Web服务器之间共享的问题。
                         Response.Cookies["sessionId"].Value = sessionId;//将Memcache的key以cookie的形式返回到浏览器端的内存中,当用户再次请求其它的页面请求报文中会以Cookie将该值再次发送服务端。
                         Response.Redirect("/Home/Index");
                        
                     }
                 }
                 Response.Cookies["cp1"].Expires = DateTime.Now.AddDays(-1);
                 Response.Cookies["cp2"].Expires = DateTime.Now.AddDays(-1);
              }
          }

        #endregion

 登录CONTROLLER不能集成BASECONTROLLER(任何用户都可访问)

posted @ 2016-12-11 10:41  ecollab  阅读(431)  评论(0编辑  收藏  举报