【问题标题】:Which of the three properties controlling the lifetime of a cookie takes precedence?控制 cookie 生命周期的三个属性中的哪一个优先?
【发布时间】:2014-09-23 22:56:33
【问题描述】:

通过将 AuthenticationProperties.IsPersistent 设置为 true,我们使 身份验证 cookie 持久化,因此用户在开始新会话时不必再次进行身份验证

CookieAuthenticationOptions.ExpireTimeSpans 控制 cookie 的有效期。

AuthenticationProperties.ExpiresUtc 设置认证到期日期

a)我不确定“身份验证的到期日期”是什么意思。也许上面的引用是说ExpiresUtc 控制在再次要求用户登录之前身份验证cookie可以用于身份验证多长时间?

b) 如果我在 a) 中的假设是正确的,那么当达到ExpireUtc 时,cookie 将过期,即使:

  • IsPersistent == true

  • 会话尚未过期且

  • cookie 还没有超过ExpireTimeSpan 设置的寿命?

c) 如果IsPersistent 设置为false,那么一旦会话到期cookie 就不再有效即使:

  • cookie 尚未超过ExpireTimeSpan 设置的寿命和

  • 身份验证还没有达到ExpireUtc设置的寿命?

d) 当 cookie 超过 ExpireTimeSpan 设置的寿命时,cookie 不再有效,即使:

  • IsPersistent == true

  • 会话尚未过期并且

  • 身份验证还没有达到ExpireUtc设置的寿命?

谢谢

【问题讨论】:

    标签: authentication asp.net-mvc-5 asp.net-identity


    【解决方案1】:

    如果AuthenticationProperties.IsPersistent 为假

    • cookie 过期设置为 Session
    • 忽略CookieAuthenticationOptions.ExpireTimeSpans
    • 忽略AuthenticationProperties.ExpiresUtc

    否则

    如果没有设置AuthenticationProperties.ExpiresUtc

    • cookie过期设置为CookieAuthenticationOptions.ExpireTimeSpans,默认为14 days

    如果有AuthenticationPropertise.ExpiresUtc设置

    • cookie 过期设置为那个
    • 忽略CookieAuthenticationOptions.ExpireTimeSpans

    【讨论】:

      【解决方案2】:

      在身份框架的情况下,有一个属性 ExpireTimeSpan ,它表明 cookie 的生命周期。根据以下示例,cookie 将在 24 小时后失效。

          public static void ConfigureOAuthSettings(IAppBuilder app)
          {
              app.UseCookieAuthentication(new CookieAuthenticationOptions
              {
                  AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                  SlidingExpiration = false,
                  ExpireTimeSpan = TimeSpan.FromHours(24)
              });}
      

      如果您想获取更详细的代码,请访问here

      【讨论】:

      • 鉴于问题的详细信息,信息不足。
      猜你喜欢
      • 2018-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多