Skip to content

DateTimeOffset.UtcNow.DateTime.Kind == DateTimeKind.Unspecified

BrentSchmaltz edited this page Mar 10, 2022 · 2 revisions

When using DateTimeOffset.

Consider the following code:

JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(expires: DateTimeOffset.UtcNow.DateTime.AddMinutes(15));

The DateTime instance passed to the ctor, will have the UTC time, but the Kind property will be DateTimeKind.Unspecified. Internally

EpochTime.GetIntDate(DateTime);

will get called where DateTime.ToUniversalTime() will be called and the UTC time will be changed.

Most likely this is not what the use wants.

The following will work as expected.

JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(expires: DateTimeOffset.DateTime.AddMinutes(15));
JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(expires: DateTime.UtcNow.AddMinutes(15));
JwtSecurityToken jwtSecurityToken = new JwtSecurityToken(expires: DateTime.Now.AddMinutes(15));
Clone this wiki locally