asp.net-core - 为什么我的 JWT 不记名身份验证在 token 显示 5 分钟后将 token 识别为过期?

标签 asp.net-core jwt aspnet-contrib

我正在使用 AspNet.Security.OpenIdConnect.Server 来发布 JWT token 并将 AuthorizationCodeLifetime 设置为 30 秒以进行测试。这是我用来设置选项的代码片段

        options.TokenEndpointPath = "/api/token";
        options.AllowInsecureHttp = true;
        options.AccessTokenHandler = new JwtSecurityTokenHandler();
        options.SigningCredentials.Add(new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256));
        options.AccessTokenLifetime = TimeSpan.FromSeconds(30);
        options.AuthorizationCodeLifetime = TimeSpan.FromSeconds(30);

返回的 token 包含:
  "expires_in": 30,

并且反序列化的 token 包含以下声明:
 "nbf": 1476915220,
 "exp": 1476915250,
 "iat": 1476915220,

如您所见,exp(过期时间)是在 iat(发布时间)之后 30 秒。然而, token 在过期 5 分钟后才会触发 401 Unauthorized。如果我们将 exp 编号插入 http://www.epochconverter.com/然后我们将看到它的计算结果为格林威治标准时间 2016 年 10 月 19 日星期三 22:14:10,即我本地时间下午 5:14:10。

以下是来自 Core 库的一些记录输出:
Microsoft.IdentityModel.Tokens.SecurityTokenExpiredException: IDX10223: Lifetime validation failed. The token is expired.
ValidTo: '10/19/2016 22:14:10'
Current time: '10/19/2016 22:19:10'.
   at Microsoft.IdentityModel.Tokens.Validators.ValidateLifetime(Nullable`1 notBefore, Nullable`1 expires, SecurityToken securityToken, TokenValidationParameters validationParameters)
   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateLifetime(Nullable`1 notBefore, Nullable`1 expires, JwtSecurityToken securityToken, TokenValidationParameters validationParameters)
   at System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler.ValidateToken(String token, TokenValidationParameters validationParameters, SecurityToken& validatedToken)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.<HandleAuthenticateAsync>d__1.MoveNext()
info: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[7]
      Bearer was not authenticated. Failure message: IDX10223: Lifetime validation failed. The token is expired.
ValidTo: '10/19/2016 22:14:10'
Current time: '10/19/2016 22:19:10'.

当然,此输出并不能证明服务器在 22:14:10 和 22:19:10 之间接受了 token 。如果我碰巧在它过期后等待了 5 分钟,然后尝试验证 token ,它就会这样做,但你必须相信我的话。我在 Postman 中进行测试,每秒单击“发送”,直到返回 401。

那么这是什么一回事?是否有一些我不知道的内置 5 分钟缓冲区?有趣的是,AccessTokenLifetime 的默认值是 5 分钟,但 token 确实反射(reflect)了我已将其更改为 30 秒。这是怎么回事?

我正在使用的相关库:
<package id="AspNet.Security.OpenIdConnect.Extensions" version="1.0.0-beta6-final" targetFramework="net452" />
<package id="AspNet.Security.OpenIdConnect.Server" version="1.0.0-beta6-final" targetFramework="net452" />
<package id="Microsoft.AspNetCore.Authentication.JwtBearer" version="1.0.0" targetFramework="net452" />

最佳答案

So, what's up? Is there some 5 minute buffer built in that I'm not aware of? What's going on?



您所说的“缓冲区”实际上是 JWT 承载中间件(由 Microsoft 开发)提供的内置功能,称为“时钟偏差”,旨在​​减轻 Web 场中时钟不同步的影响。

如您所知,the default value is set to 5 minutes ,但可以通过 JwtBearerOptions.TokenValidationParameters.ClockSkew 更改.

关于asp.net-core - 为什么我的 JWT 不记名身份验证在 token 显示 5 分钟后将 token 识别为过期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40142548/

相关文章:

amazon-web-services - Cognito 刷新 token 是 "valid"JSON Web token 吗?

oauth-2.0 - 使用 Identity Core 颁发不记名 JWT token

asp.net-core - ASOS - 当具有单独的授权服务器和资源服务器时, token 验证不起作用

c# - 带有 AsyncLocal 与 Scope 服务的单例

python - Django + Auth0 JWT 认证拒绝解码

c# - 如何在 Blazor WebAssembly-PWA 中运行离线数据库使用?

amazon-web-services - 本地开发 - 使用 AWS Cognito 和 ALB 进行身份验证后在 Node.js 应用程序中授权

asp.net-core - 带有 ASP.NET Identity 3 的 JWT 不记名 token

c# - ASP.NET Core (MVC 6) 中的路由消歧

asp.net-core - 数据库中已经有一个名为 AspNetRoles 的对象。 ( Entity Framework 核心)