【问题标题】:[OAuth2 authorization server]refresh token's expire time need different with access token?[OAuth2授权服务器]刷新令牌的过期时间需要与访问令牌不同吗?
【发布时间】:2015-09-01 02:39:33
【问题描述】:

我通过这个tutorial学习OAuth2,然后我发现刷新令牌的过期时间和访问令牌一样,对吗?

【问题讨论】:

    标签: asp.net oauth oauth-2.0 owin


    【解决方案1】:

    一般来说这没有多大意义:refresh_token 的存在是为了让客户端在当前access_token 过期时获得新的access_token。如果到那时refresh_token 也已过期,则客户端无法使用它,因此它是无用的。

    虽然有一个(或多或少)边缘情况有用:当资源服务器在到期之前主动拒绝 access_token 时,客户端现在可以返回授权服务器以获取新的 access_token .

    【讨论】:

      【解决方案2】:

      确实如此:OWIN/Katana 内置的 OAuth2 授权服务器颁发的刷新令牌始终与访问令牌具有相同的到期日期;即使您在调用IOwinContext.Authentication.SignIn(identity, properties) 时在AuthenticationProperties 中指定了显式ExpiresUtc 属性

      https://github.com/yreynhout/katana-clone/blob/master/src/Microsoft.Owin.Security.OAuth/OAuthAuthorizationServerHandler.cs#L333

      由于@Hans 提到的原因,这并不是很方便,但您可以在AuthenticationTokenProvider.CreateAsync(您用于OAuthAuthorizationServerOptions.RefreshTokenProvider 的类)中覆盖此行为:

      只需将context.Ticket.Properties.ExpiresUtc设置为您选择的到期日期,刷新令牌就会发出不同的到期日期:

      public class RefreshTokenProvider : AuthenticationTokenProvider {
          public override void Create(AuthenticationTokenCreateContext context) {
              context.Ticket.Properties.ExpiresUtc = // set the appropriate expiration date.
      
              context.SetToken(context.SerializeTicket());
          }
      }
      

      您还可以查看AspNet.Security.OpenIdConnect.Server,它是 OWIN/Katana 提供的 OAuth2 授权服务器的一个分支,具有原生 RefreshTokenLifetimehttps://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev

      app.UseOpenIdConnectServer(options => {
          // Essential properties omitted for brevity.
          // See https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/dev/samples/Mvc for more information.
      
          // RefreshTokenLifetime allows you to define a lifetime specific to refresh tokens,
          // which is totally independent of the lifetime used for access tokens.
          options.RefreshTokenLifetime = TimeSpan.FromDays(14);
      });
      

      如果您需要帮助,请随时联系我。

      【讨论】:

      • 我知道了,非常感谢。但是为什么武士刀将它们设置为相同的呢?
      猜你喜欢
      • 2022-10-23
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 2018-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      相关资源
      最近更新 更多