JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同

编程入门 行业动态 更新时间:2024-10-25 09:24:58
本文介绍了JwtSecurityToken 与 SecurityTokenDescriptor 中的日期不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I'm implementing mechanizm to manage tokens in my application and I 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 中的日期不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 04:47:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   JwtSecurityToken   SecurityTokenDescriptor

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!