2013-04-24 62 views
0

我使用ASP.net MVC4与SimpleMemberShip。Simplemembership登录和RemeberMe ASP.Net MVC4

我只是想存储用户名,如果记住我复选框勾选并重新从cookie中加载它。

登录工作正常,RememberMe设置为true。但Request.Cookies [FormsAuthentication.FormsCookieName]始终为空。我很困惑这应该如何工作。

登录控制器:

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public ActionResult Index(LoginModel model, string returnUrl) 
    { 
     bool RememberMe = model.RememberMe == "on" ? true : false; 
     if (WebSecurity.Login(model.UserName, model.Password, persistCookie: RememberMe)) 
     { 
      return RedirectToLocal(returnUrl); 
     } 

     // If we got this far, something failed, redisplay form 
     ModelState.AddModelError("", "The user name or password provided is incorrect."); 
     return View(model); 
    } 

登录页面控制器:

[AllowAnonymous] 
    public ActionResult Index(string returnUrl) 
    { 
     // load user name 
     HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 


     if (authCookie != null) 
     { 
      FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); 
      ViewBag.Username = Server.HtmlEncode(ticket.Name); 
      ViewBag.RememberMeSet = true; 
     } 
     else 
     { 
      ViewBag.RememberMeSet = false; 
     } 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 
+0

如果你只是想在当前登录用户的用户名,为什么不干脆用WebSecurity.CurrentUserName属性。 http://msdn.microsoft.com/en-us/library/webmatrix.webdata.websecurity.currentusername(v=vs.111).aspx – 2013-04-24 12:44:04

+0

我只是想通过formsauthentication将cookie中存储的用户名加载到用户名文本框中在登录页面上。 – Simon 2013-04-24 16:50:48

+0

如果您尝试在登录页面上使用它,那么我假定该用户未登录。如果他们未登录,则该Cookie将为空。如果他们已经登录,那么你正在尝试做的工作。但是,如前所述,如果他们登录,更容易调用WebSecurity.CurrentUserName来获取用户名,而不是试图从cookie中提取它。 – 2013-04-24 17:36:38

回答

1

我希望得到的用户名,点击 “记住我” 复选框保存。我现在明白,除非登录,否则cookie是空的,因此它在登录页面上没有用处。为了参考,我在下面添加了我的解决方案

手柄登录请求控制器:

 [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public ActionResult Index(LoginModel model, string returnUrl) 
     { 
      // handle remembering username on login page 
      bool RememberMe = model.RememberMe == "on" ? true : false; 
      HttpCookie existingCookie = Request.Cookies["xxx_username"]; 

      if (RememberMe) 
      { 
       // check if cookie exists and if yes update 
       if (existingCookie != null) 
       { 
        // force to expire it 
        existingCookie.Expires = DateTime.Today.AddMonths(12); 
       } 
       else 
       { 
        // create a cookie 
        HttpCookie newCookie = new HttpCookie("xxx_username", model.UserName); 
        newCookie.Expires = DateTime.Today.AddMonths(12); 
        Response.Cookies.Add(newCookie); 
       } 
      } 
      else 
      { 
       // remove cookie 
       if (existingCookie != null) 
       { 
        Response.Cookies["xxx_username"].Expires = DateTime.Now.AddDays(-1); 
       } 
      } 

      if ((!string.IsNullOrEmpty(model.UserName)) && (!string.IsNullOrEmpty(model.Password))) 
      { 
       if (WebSecurity.Login(model.UserName, model.Password, RememberMe)) 
       { 
        return RedirectToLocal(returnUrl); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      TempData["ErrorMsg"] = "Login failed"; 
      return View(model); 
     } 

显示登录页面控制器:

[AllowAnonymous] 
    public ActionResult Index(string returnUrl) 
    { 
     // load user name 
     HttpCookie existingCookie = Request.Cookies["xxx_username"]; 
     if (existingCookie != null) 
     { 
      ViewBag.Username = existingCookie.Value; 
      ViewBag.RememberMeSet = true; 
     } 
     else 
     { 
      ViewBag.RememberMeSet = false; 
     } 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    }