简体   繁体   English

如何更改jwt.io令牌中的'nbf'值:C#

[英]How to change 'nbf' value in jwt.io token : C#

For some reason I want to change nbf payload value in my jwt token . 由于某些原因,我想更改jwt令牌中的 nbf有效负载值。 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()),

Where I'm doing mistakes, nbf value not updated with my value. 我在做错的地方, nbf值未更新为我的值。 Actual nbf value is 1487049869 (system generated datetime) but my value is 63622665869 (-10 minutes less than current datetime). 实际的nbf值为1487049869 (系统生成的日期时间),但我的值为63622665869 (比当前日期时间短-10分钟)。

two things come to my mind: 我想到两件事:

  1. 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); 

then the fourth parameter is the desired nbf (not before). 那么第四个参数是所需的nbf(不在此之前)。 Actually you don't need to add a nbf claim manually, as it is one of the standard fields in a JWT 实际上,您不需要手动添加nbf声明,因为它是JWT中的标准字段之一

  1. your timestamp seems odd to me 您的时间戳对我来说似乎很奇怪

my value is 63622665869 (-10 minutes less than current datetime). 我的值是63622665869(比当前日期时间短-10分钟)。

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) JWT中的时间戳是UNIX时间戳,从1970年1月1日00:00 UTC开始计数: https : //tools.ietf.org/html/rfc7519#section-4.1.4解释了数字日期用于exp声明(以及对于nbf(不早于)和iat(在发出)索赔)

https://tools.ietf.org/html/rfc7519#section-2 defines the numeric date: https://tools.ietf.org/html/rfc7519#section-2定义数字日期:

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. 一个JSON数值,表示从1970-01-01T00:00:00Z UTC到指定的UTC日期/时间为止的秒数,而忽略了leap秒。

so JWT would interpret your value (63622665869) as 02/14/3986 @ 10:44am (UTC) or it is not accepted at all. 因此,JWT会将您的值(63622665869)解释为02/14/3986 @ 10:44 am(UTC)或完全不接受。

there are several websites where you can check/convert your timestamp, eg this one: http://www.unixtimestamp.com/ 您可以在多个网站上查看/转换时间戳,例如: http : //www.unixtimestamp.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM