如何更改jwt.io令牌中的'nbf'值:C# [英] How to change 'nbf' value in jwt.io token : C#
问题描述
出于某种原因,我想更改我的 jwt令牌中的nbf
有效负载值.我试图增加价值,但无法实现.
For some reason I want to change nbf
payload value in my jwt token. I am trying to add value but unable to achieve it.
DateTime original = DateTime.Now;
original = original.AddMinutes(-10);
var seconds = original.Subtract(DateTime.MinValue).TotalSeconds;
var claimsIdentity = new ClaimsIdentity(new List<Claim>()
{
new Claim("email",sresult.Properties["mail"][0].ToString()),
new Claim("sub", accountName),
new Claim("myv",seconds.ToString()),
new Claim("nbf",seconds.ToString()),
我在出错的地方,nbf
值未更新为我的值.实际nbf
值是1487049869
(系统生成的日期时间),但我的值是63622665869
(比当前日期时间短-10分钟).
Where I'm doing mistakes, nbf
value not updated with my value. Actual nbf
value is 1487049869
(system generated datetime) but my value is 63622665869
(-10 minutes less than current datetime).
推荐答案
我想到两件事:
-
您在这里没有显示太多代码,因此我看不到令牌是如何生成的,但是如果您有类似这样的内容:
you didn't show much of your code here, so I can't see how the token is generated, but if you have something like this:
var token = new JwtSecurityToken(_issuer, audienceId, data.Identity.Claims, issued.Value.UtcDateTime, expires.Value.UtcDateTime, signingKey);
则第四个参数是所需的nbf(不在此之前).实际上,您不需要手动添加nbf声明,因为它是JWT中的标准字段之一
then the fourth parameter is the desired nbf (not before). Actually you don't need to add a nbf claim manually, as it is one of the standard fields in a JWT
- 您的时间戳对我来说似乎很奇怪
我的值是63622665869(比当前日期时间短-10分钟).
my value is 63622665869 (-10 minutes less than current datetime).
JWT中的时间戳是从1970年1月1日00:00 UTC开始计数的UNIX时间戳:
the timestamps in JWT are UNIX timestamps counting from 01.01.1970 00:00 UTC: https://tools.ietf.org/html/rfc7519#section-4.1.4 explains that a numeric date is used for the exp claim (and also for the nbf (not before) and iat (issued at) claims)
https://tools.ietf.org/html/rfc7519#section-2 定义数字日期:
一个JSON数值,表示从 1970-01-01T00:00:00Z UTC直到指定的UTC日期/时间,忽略 秒.
A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds.
所以JWT会解释您的价值(63622665869) 如02/14/3986 @ 10:44 am(UTC) 或根本不接受.
so JWT would interpret your value (63622665869) as 02/14/3986 @ 10:44am (UTC) or it is not accepted at all.
有多个网站可以在其中检查/转换时间戳,例如: http://www.unixtimestamp .com/
there are several websites where you can check/convert your timestamp, e.g this one: http://www.unixtimestamp.com/
这篇关于如何更改jwt.io令牌中的'nbf'值:C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!