响应cookie没有真正设置?

时间:2017-10-26 16:25:25

标签: c# asp.net-mvc cookies

我将用户电子邮件存储在cookie中(取决于登录页面上的记住我状态复选框),使用以下两种简单方法:

    private void UpdateEmailCookie(LoginModel loginModel)
    {
        if (loginModel.RememberMe.GetValueOrDefault())
        {
            if (Response.Cookies[EmailCookieName] == null)
            {
                var httpCookie = new HttpCookie(EmailCookieName, loginModel.Email);
                Response.Cookies.Add(httpCookie);
            }
            else
            {
                Response.Cookies[EmailCookieName].Value = loginModel.Email;
            }
        }
        else
        {
            Response.Cookies.Remove(EmailCookieName);
        }
    }

    private void LoadEmailCookie(LoginModel loginModel)
    {
        if (Request.Cookies[EmailCookieName] != null)
        {
            loginModel.Email = Request.Cookies[EmailCookieName].Value;
            loginModel.RememberMe = true;
        }
        else
        {
            loginModel.Email = null;
            loginModel.RememberMe = false;
        }
    }

分别用于:

    // GET: /Account/Login
    [AllowAnonymous]
    [HttpGet]
    public ActionResult Login()
    {
        if (Request.IsAuthenticated)
        {
            return RedirectToAction("Index", "Home");
        }

        var loginModel = new LoginModel();

        LoadEmailCookie(loginModel);

        return View(loginModel);
    }

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginModel loginModel, string returnUrl)
    {
         [Code for validating the email and password]
         [If valid it goes below]

         UpdateEmailCookie(loginModel);

         await SignInManager.SignInAsync(user, false, true);

         return RedirectToLocal(returnUrl);
    }

当用户决定让应用程序记住其密码时似乎很好,但是当他或她取消选中该框时(因此将RememberMe设置为false),似乎电子邮件仍然保存在cookie中,因此仍显示加载视图时的向上

任何想法为什么我的cookie在实际调用Response.Cookies.Remove(EmailCookieName);时保留信息。

[编辑] 正如在接受的答案中所提到的,我没有设置Expires属性,这基本上是奇怪的,告诉cookie是否应该删除。 此外,没有必要检查cookie集合上的null ...如果设计有点违反直觉,似乎很多人希望删除cookie,而他们需要使它过期。

我的工作解决方案:

    private void UpdateEmailCookie(LoginModel loginModel)
    {
        if (loginModel.RememberMe.GetValueOrDefault())
        {
            Response.Cookies[EmailCookieName].Value = loginModel.Email;
            Response.Cookies[EmailCookieName].Expires = DateTime.Now.AddYears(1);
        }
        else
        {
            Response.Cookies[EmailCookieName].Expires = DateTime.MinValue;
        }
    }

    private void LoadEmailCookie(LoginModel loginModel)
    {
        if (!string.IsNullOrEmpty(Request.Cookies[EmailCookieName]?.Value))
        {
            loginModel.Email = Request.Cookies[EmailCookieName].Value;
            loginModel.RememberMe = true;
        }
        else
        {
            loginModel.Email = null;
            loginModel.RememberMe = false;
        }
    }

1 个答案:

答案 0 :(得分:1)

您只是从您这边删除cookie,但用户的浏览器仍然拥有它。 除了你正在做的事情,你应该让cookie过期。