Filip Szesto
Filip Szesto

Reputation: 163

JwtSecurityToken different dates than in SecurityTokenDescriptor

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

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

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

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.

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?

Upvotes: 3

Views: 2437

Answers (1)

Progra-Marcol
Progra-Marcol

Reputation: 11

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

Instead of using

IssuedAt = DateTime.UtcNow

You want to be using

NotBefore = DateTime.UtcNow

It seems like

SecurityToken.ValidFrom

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

Hope this helps.

Upvotes: 1

Related Questions