本文介绍了JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施 mechanizm 来管理我的应用程序中的令牌,并且我使用这样的代码来创建 JwtSecurityToken

I'm implementing mechanizm to manage tokens in my application andI use such code to create JwtSecurityToken

var securityTokenDescriptor = new SecurityTokenDescriptor()
{
    Subject = claims,
    SigningCredentials = signingCredentials,
    Expires = DateTime.UtcNow.AddMinutes(ACCESS_TOKEN_LENGHT_MINUTES),
    IssuedAt = DateTime.UtcNow
};

var tokenJwt = tokenHandler.CreateJwtSecurityToken(securityTokenDescriptor);

意外地,'tokenJwt' 中的日期与 securityTokenDescriptor 中的日期不同

And unexpectedly dates in 'tokenJwt' are different, than in securityTokenDescriptor

带有Expires"的ValidTo"和带有IssuedAt"的ValidFrom"都相差一小时.

Both 'ValidTo' with 'Expires' and 'ValidFrom' with 'IssuedAt' differ in exactly one hour.

我想这与夏季/冬季时间(目前是冬季时间)之间的时间变化有关,或者与我住在 UTC +1:00 时区有关.

I suppose it's connected with changing time between Summer/Winter times (Currently it's a winter time) or with fact, that I live in UTC +1:00 time zone.

我尝试同时使用 DateTime.Now 和 DateTime.UtcNow,但它们都存在相同的问题

I tried using both DateTime.Now and DateTime.UtcNow but there is the same problem with both of them

有谁知道为什么会这样并且知道这些问题的解决方案吗?

Does anyone knows why it is happening like this and knows the solution of these problem?

推荐答案

我遇到了类似的问题,我已经找到了解决方案.

I have run into a similar problem and I have found a solution.

代替使用

IssuedAt = DateTime.UtcNow

您想使用

NotBefore = DateTime.UtcNow

好像

SecurityToken.ValidFrom

从 NotBefore 字段中获取它的值,如果您不提供它,它将自动生成一个.

Takes its value from the NotBefore field, and if you don't supply one it will generate one automatically.

希望这会有所帮助.

这篇关于JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 01:07