【发布时间】:2015-09-01 02:39:33
【问题描述】:
我通过这个tutorial学习OAuth2,然后我发现刷新令牌的过期时间和访问令牌一样,对吗?
【问题讨论】:
标签: asp.net oauth oauth-2.0 owin
我通过这个tutorial学习OAuth2,然后我发现刷新令牌的过期时间和访问令牌一样,对吗?
【问题讨论】:
标签: asp.net oauth oauth-2.0 owin
一般来说这没有多大意义:refresh_token 的存在是为了让客户端在当前access_token 过期时获得新的access_token。如果到那时refresh_token 也已过期,则客户端无法使用它,因此它是无用的。
虽然有一个(或多或少)边缘情况有用:当资源服务器在到期之前主动拒绝 access_token 时,客户端现在可以返回授权服务器以获取新的 access_token .
【讨论】:
确实如此:OWIN/Katana 内置的 OAuth2 授权服务器颁发的刷新令牌始终与访问令牌具有相同的到期日期;即使您在调用IOwinContext.Authentication.SignIn(identity, properties) 时在AuthenticationProperties 中指定了显式ExpiresUtc 属性
由于@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 授权服务器的一个分支,具有原生 RefreshTokenLifetime:https://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);
});
如果您需要帮助,请随时联系我。
【讨论】: