小编Dev*_*lch的帖子

如何使用 .NET Core 使用 UTC 时间验证 JWT

目前我正在使用 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 进行令牌验证?或者有没有其他方法可以解决这个问题?

c# datetime jwt .net-core

6
推荐指数
2
解决办法
2688
查看次数

标签 统计

.net-core ×1

c# ×1

datetime ×1

jwt ×1