0

我正在使用带有 SimpleMemberShip 的 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();
    }
4

1 回答 1

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();
    }
于 2014-11-10T18:55:37.773 回答