目前我正在使用 JWT-Bearer-Authentication 对 ASP.NET-Core WebApi 进行编程。
为了使 API 可从不同的时区访问,我使用以下模式将JWT 中的字段nbf(notBefore) 和exp(expires) 设置为 UTC 时间戳:
var utcNow = DateTime.SpecifyKind(DateTime.UtcNow, DateTimeKind.Unspecified);
...
var tokenOptions = new JwtSecurityToken(
notBefore: utcNow,
expires: utcNow.AddSeconds(3600),
);
...
Run Code Online (Sandbox Code Playgroud)
对于令牌生成,一切都很好,nbf并且exp包含代表当前 UTC 时间的 UNIX 时间戳。
但是在进行令牌验证时,一切都在 5 分钟(我的时钟偏差设置)内有效,然后我只能从 API 获得 401,因为令牌验证是使用我在德国的当前时区完成的。
有没有办法在 .NET-Core 中设置 JwtAuthentication-Middleware 以使用 UTC-Time 进行令牌验证?或者有没有其他方法可以解决这个问题?