首页 文章

Identity Server 3刷新令牌在客户端配置中设置的到期时间之前到期

提问于
浏览
7

我正在为我的一个Identity Server 3客户端使用授权代码流,它配置如下:

ClientId = "tripgalleryauthcode",
ClientName = "Trip Gallery",
Flow = Flows.AuthorizationCode, 
AllowAccessToAllScopes = true,
RequireConsent = false,

// redirect = URI of our callback controller in the IOS application
RedirectUris = new List<string>
{
     "somecallbackuri"
},           

ClientSecrets = new List<Secret>()
{
    "somesecret"
},

// refresh token options
AccessTokenType = AccessTokenType.Jwt,
AccessTokenLifetime = 120,
RefreshTokenUsage = TokenUsage.OneTimeOnly,
RefreshTokenExpiration = TokenExpiration.Absolute,
AbsoluteRefreshTokenLifetime = 360,

如您所见,它被配置为在2分钟内使访问令牌到期,在6分钟内使刷新令牌到期 . 我这样做是因为我想尝试在更短的时间范围内调试问题而不是我在 生产环境 中使用的那个:刷新令牌15天,访问令牌1小时 . 我们注意到由于某种原因,今天发布的刷新令牌明天不起作用 . 这就是为什么我决定减少时间,这就是发生的事情:

  • At 1:05 PM 我发了刷新令牌请求并收到了新的refres和访问令牌

  • 现在我希望我的刷新令牌到期 at 1:11 PM

  • At 1:10 PM 我使用refresh_token授权类型调用令牌 endpoints ,尝试获取新访问权限并刷新令牌 . 发生的是我收到HTTP 400错误,说这是invalid_grant .

我注意到了更多 . 发生的事情是访问令牌到期后2分钟我得到400错误 . 它说刷新令牌无效 .

这是Identity Server的日志 .

w3wp.exe Information: 0 : 2016-11-23 10:56:15.802 +00:00 [Information] Start token request
w3wp.exe Information: 0 : 2016-11-23 10:56:15.802 +00:00 [Information] Client secret id found: "tripgalleryauthcode"
w3wp.exe Information: 0 : 2016-11-23 10:56:15.802 +00:00 [Information] Client validation success
w3wp.exe Information: 0 : 2016-11-23 10:56:15.802 +00:00 [Information] Start token request validation
w3wp.exe Information: 0 : 2016-11-23 10:56:15.802 +00:00 [Information] Start validation of refresh token request
w3wp.exe Warning: 0 : 2016-11-23 10:56:15.802 +00:00 [Warning] "Refresh token has expired"
 "{
  \"ClientId\": \"tripgalleryauthcode\",
  \"ClientName\": \"Trip Gallery\",
  \"GrantType\": \"refresh_token\",
  \"RefreshToken\": \"d12f50289e5cded13082de989a64ac01\",
  \"Raw\": {
    \"grant_type\": \"refresh_token\",
    \"refresh_token\": \"d12f50289e5cded13082de989a64ac01\"
  }
}"
w3wp.exe Information: 0 : 2016-11-23 10:56:15.818 +00:00 [Information] End token request
w3wp.exe Information: 0 : 2016-11-23 10:56:15.818 +00:00 [Information] Returning error: invalid_grant

我真的很想知道导致该行为的原因以及导致我的到期令牌在截止日期之前到期的原因 .

1 回答

  • 1

    发生这种情况的原因是因为JWT内置了一个时钟偏移功能,可以保护您免受同步时钟的影响 . 如果没有这个,你可能会遇到令牌无效的问题 .

    默认值为5分钟 - 这会影响 access_token 以及 refresh_token .

    您可以在 IdentityServer4.AccessTokenValidation.CombinedAuthenticationOptions 中使用 JwtBearerOptions.TokenValidationParameters.ClockSkew 更改此值

    此行为也在official JWT Draft中指定:

    实施者可以提供一些小的余地,通常不超过几分钟,以解决时钟偏差 . 它的值必须是一个包含IntDate值的数字 . 这种说法是可选的 .

相关问题