Simplemembership登录和RemeberMe ASP.Net MVC4

时间:2013-04-24 11:08:26

标签: asp.net-mvc simplemembership

我正在使用带有SimpleMemberShip的ASP.net MVC4。

如果勾选了“记住我”复选框,我只想存储用户名,然后从cookie中重新加载。

登录正常,RememberMe设置为true。但Request.Cookies [FormsAuthentication.FormsCookieName]始终为null。我对这应该如何运作感到困惑。

登录控制器:

    [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();
    }

1 个答案:

答案 0 :(得分:1)

我想通过点击“记住我”复选框来保存用户名。我现在知道cookie是null,除非登录,因此它在登录页面上没用。作为参考,我在下面添加了我的解决方案。

处理登录请求控制器:

        [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();
    }
相关问题