I am currently using the JwtSecurityToken class in System.IdentityModels.Tokens namespace. I create a token using the following:
DateTime expires = DateTime.Utc
The problem is related ClockSkew
. Normally, the validation libraries (at least the MS one) compensate for clock skew. ClockSkew
default value is 5 minutes. See some answer here
You can change ClockSkew
in TokenValidationParameters
:
var tokenValidationParameters = new TokenValidationParameters
{
//...your setting
// set ClockSkew is zero
ClockSkew = TimeSpan.Zero
};
app.UseJwtBearerAuthentication(new JwtBearerOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
TokenValidationParameters = tokenValidationParameters
});
Happy coding!