skip to content

In ASP.NET Core JWT bearer authentication, why is a token whose exp has just passed still accepted, and which TokenValidationParameters settings govern token lifetime checks?

level: middleimportance: should knowfreq 48%

answer

  1. tolerance for clock drift
  2. ClockSkew defaults to 5 minutes
  3. applies to exp and nbf
  4. RequireExpirationTime defaults to true
  5. IDX10223 means expired

basics

~10 s

TokenValidationParameters.ClockSkew defaults to five minutes, so an expired token stays valid until exp plus five minutes. ValidateLifetime enables the exp and nbf checks, and RequireExpirationTime rejects tokens with no exp.

solid answer

~40 s

Lifetime validation in `JwtBearer` is done by the token handler against `TokenValidationParameters`. `ValidateLifetime` (default `true`) checks `exp` and `nbf`, but both comparisons apply `ClockSkew`, which defaults to **five minutes** (`TokenValidationParameters.DefaultClockSkew`, 300 seconds). A token counts as expired only when `exp` is earlier than now minus the skew, and as not yet valid only when `nbf` is later than now plus the skew. That is why a test token with a one-minute lifetime keeps working for about six. `RequireExpirationTime` (default `true`) rejects tokens with no `exp` (IDX10225). Lowering `ClockSkew` to a few seconds or zero tightens the window but makes small clock differences between the identity provider and the API reject fresh tokens; turning `ValidateLifetime` off accepts expired tokens forever.

code

csharp · 5 lines
csharp
// Integration-test host: make expiry assertions exact
builder.Services.PostConfigure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
{
    options.TokenValidationParameters.ClockSkew = TimeSpan.Zero;
});

go deeper

for a junior

Recall that ClockSkew defaults to five minutes and that it is why an expired token is briefly still accepted.

for a middle

Explain both comparisons with the skew, what ValidateLifetime and RequireExpirationTime do, and which exceptions and IDX codes a lifetime failure reports.

for a senior

Size ClockSkew against token lifetime and clock synchronisation, keep it zero only in tests, and refuse ValidateLifetime = false as a fix.

for a principal

Treat token lifetime, skew and revocation as one budget: how long a leaked token stays usable, and what each lever costs in false rejections.

## The five-minute surprise A team writes an integration test for the mobile banking API: mint a token valid for 30 seconds, wait a minute, call the API, expect 401. The call succeeds. Nothing is broken; the test has met **clock skew**. `TokenValidationParameters.ClockSkew` is a tolerance for clocks that disagree. The identity provider stamps `exp` (expiry) and often `nbf` (not before) using its own clock; the API compares them with its own clock. Two machines can be a few seconds or minutes apart, so the comparison is padded. The default is `TokenValidationParameters.DefaultClockSkew`: **300 seconds**. ## How the lifetime check works When `ValidateLifetime` is `true` (the default), the IdentityModel validators run two comparisons: 1. **Expired** if `exp` < now − `ClockSkew`. 2. **Not yet valid** if `nbf` > now + `ClockSkew`. So with the defaults a token is accepted from five minutes before its `nbf` until five minutes after its `exp`. On failure the handler records exceptions such as `SecurityTokenExpiredException` (IDX10223, "Lifetime validation failed. The token is expired") or `SecurityTokenNotYetValidException`, and the 401 challenge carries a description such as `The token expired at '...'`. ## The lifetime settings | Setting | Default | Effect | |---|---|---| | `ValidateLifetime` | `true` | turns the `exp` / `nbf` comparisons on | | `ClockSkew` | 5 minutes | tolerance added to both comparisons | | `RequireExpirationTime` | `true` | a token with no `exp` fails (IDX10225) | | `LifetimeValidator` | `null` | a delegate that replaces the default lifetime check entirely | ## Choosing a ClockSkew - **Keep the default** when access tokens live for an hour or so; five extra minutes is small next to the lifetime. - **Lower it** (for example to 30 seconds) when tokens are deliberately short-lived, such as a five-minute token for a payment confirmation, where five extra minutes would double its life. Make sure servers synchronise their clocks first. - **`TimeSpan.Zero` in tests** makes expiry assertions exact; in production it turns every few seconds of drift between provider and API into rejected fresh tokens (`nbf` in the "future") or early expiry. - **Never set `ValidateLifetime = false`** to cure such errors: it accepts a stolen token forever. ## What the lifetime does not do - It does not end a session at sign-out. A JWT is checked locally against its own claims; the API keeps accepting it until `exp` plus skew. Revoking tokens early is a separate design problem. - It does not refresh anything. The handler only reads `exp`; obtaining a new token is the client's job. - It is evaluated on every request, not once. The handler also copies the token's validity window into the authentication properties (`IssuedUtc`, `ExpiresUtc`), but with no cookie involved nothing is extended. ## Setting it ```csharp builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = "https://id.example.com/"; options.Audience = "mobile-banking-api"; options.TokenValidationParameters.ClockSkew = TimeSpan.FromSeconds(30); }); ``` Changing one property on the existing `TokenValidationParameters` keeps every other default; assigning a new object starts again from that type's defaults, which also include a five-minute `ClockSkew`.

  • A token's nbf is 20 seconds in the future when it reaches the API. Is it rejected?
    Not with the defaults. The not-before check fails only when `nbf` is later than now plus `ClockSkew`, and five minutes covers 20 seconds. With `ClockSkew = TimeSpan.Zero` the same token fails as not yet valid, which is the classic symptom of a provider whose clock runs ahead of the API's.
  • Why not replace the lifetime logic with your own LifetimeValidator?
    You can, but the delegate replaces the default check entirely, including the `ClockSkew` handling and the specific exceptions the challenge reports. It suits a genuinely different rule; for a tighter window, adjusting `ClockSkew` keeps the built-in behaviour and diagnostics.

  • A JWT is rejected the instant its exp passes.
  • ClockSkew defaults to zero, so expiry is exact unless you add tolerance.
  • Set ValidateLifetime = false when clocks drift between servers.
  • ClockSkew only applies to exp, never to nbf.
  • Signing out of the client stops the API accepting the token.