Disabling Clock Skew in ASP.NET Core 3.1 JWT Authentication: Unresolved Behavior
0 reputation · 10 Mar 2021, 19:19 UTC
0 reputation · 10 Mar 2021, 19:19 UTC
ASP.NET Core 3.1’s JWT bearer middleware imposes a hard‑coded five‑minute clock skew that cannot be overridden via configuration or code. This default leeway allows tokens that have technically expired to remain valid for up to five minutes, potentially exposing applications to stale credential usage.
The primary objective is to enforce strict token expiration in environments running ASP.NET Core 3.1, where no documented API exists to disable the skew. Custom validation logic can be added, but its interaction with other middleware components and its impact on overall authentication flow remain unclear.
Key questions:
35275 reputation · 10 Mar 2021, 20:14 UTC
In ASP.NET Core 3.1, the five-minute clock skew is not hard-coded in a way that prevents modification. It is a default value within the TokenValidationParameters class. To enforce strict token expiration and remove the five-minute leeway, set the ClockSkew property to TimeSpan.Zero during the configuration of the JWT Bearer middleware.
Update your Startup.cs (or wherever your authentication services are configured) within the AddJwtBearer block:
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
// Other validation parameters (Issuer, Audience, etc.)
ValidateLifetime = true,
ClockSkew = TimeSpan.Zero
};
});
While ClockSkew = TimeSpan.Zero is the standard method, you can implement additional strictness using the JwtBearerEvents pipeline. This is useful if you need dynamic skew logic or additional logging when a token is rejected specifically due to expiration.
Injecting logic into OnTokenValidated is a supported extension point and does not compromise middleware stability, provided you handle the context correctly. If a token fails your custom check, you must call context.Fail() to halt the authentication pipeline.
options.Events = new JwtBearerEvents
{
OnTokenValidated = context =>
{
var expClaim = context.Principal.FindFirst("exp")?.Value;
// Perform additional manual time checks here if necessary
return Task.CompletedTask;
}
};
Setting clock skew to zero introduces a strict dependency on time synchronization. If the resource server's clock is even slightly ahead of the token issuer's clock, valid tokens may be rejected immediately upon issuance.
ClockSkew = TimeSpan.Zero to the configuration.exp (expiration) claim set to 1 minute in the past.401 Unauthorized response immediately, rather than allowing the default 5-minute grace period.Diagnostic Note: If the skew persists after this change, verify that TokenValidationParameters are not being overwritten by a second configuration call or a custom IConfigureOptions<JwtBearerOptions> implementation elsewhere in the dependency injection container.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.