【问题标题】:Response cookie not really setup?响应cookie没有真正设置?
【发布时间】:2017-10-27 02:45:23
【问题描述】:

我使用以下两种简单的方法将用户电子邮件存储在 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;
        }
    }

【问题讨论】:

    标签: c# asp.net-mvc cookies


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2018-01-31
      • 2014-11-07
      • 2019-03-21
      • 2012-03-25
      • 1970-01-01
      • 2016-08-17
      相关资源
      最近更新 更多