【问题标题】:Setting expiration for cookie on claims based authentication在基于声明的身份验证中设置 cookie 的过期时间
【发布时间】:2019-09-19 03:47:12
【问题描述】:

我得到了一个标准的 MVC5 Web 应用程序,对模板的登录进行了一些修改。

我试图在我登录时创建的 cookie 上设置 30 分钟的过期时间

这是我的登录操作

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        var user = AccountDomain.CheckUserLogin(model.UserName, model.Password);

        if (user != null)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

            var claims = new List<Claim>
            {
                new Claim("UserName", user.UserName),
                new Claim("FirstName", user.FirstName ?? ""),
                new Claim("LastName", user.LastName ?? "")
            };

            var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

            var claimsPrincipal = new ClaimsPrincipal(identity);

            Thread.CurrentPrincipal = claimsPrincipal;

            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, identity);

            return RedirectToLocal(returnUrl);
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password.");
        }

        return View(model);
    }

我试过这样做

var exp = new DateTimeOffset().AddMinutes(5);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe, ExpiresUtc = exp }, identity);

但 cookie 声明过期:当浏览会话结束时

如果在登录页面上选中了“记住我”,则 IsPersistent 将为真,并将 cookie 的到期时间设置为从登录时间算起的 14 天。

如何手动设置cookie的过期时间?

【问题讨论】:

    标签: cookies asp.net-mvc-5 claims-based-identity


    【解决方案1】:

    您应该有一个包含以下代码的 StartUp.cs 配置文件:

    app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                   ExpireTimeSpan = TimeSpan.FromDays(5),
                   SlidingExpiration = true
                }
            });
    

    ExpireTimeSpan 让您能够手动设置过期时间。

    【讨论】:

    • ExpireTimeSpan 应该在提供者之外。
    【解决方案2】:

    ExpireTimeSpan 将设置持久登录的过期时间。但是,如果您想支持两种类型的登录,这不是您想要的。这是一种适用于正常登录且不会破坏持久登录的解决方案:User logout after non-persistent login in Asp.Net Identity 2

    【讨论】:

      【解决方案3】:

      在 Startup.Auth.cs 中设置 ExpireTimeSpan,如下所示。

          app.UseCookieAuthentication(new CookieAuthenticationOptions
              {
                  AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                  LoginPath = new PathString("/Account/Login"),
                  Provider = new CookieAuthenticationProvider
                  {
                      xxx...
                  },
                  ExpireTimeSpan = TimeSpan.FromDays(7),
                  SlidingExpiration = false
      

      【讨论】:

        猜你喜欢
        • 2020-07-22
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-14
        • 2020-10-08
        • 2015-04-15
        相关资源
        最近更新 更多