Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

An expired JWT is still accepted by an ASP.NET Core API

ErrorToken expired minutes ago but the API still returns 200

Short answer

JwtBearer validates the exp claim, but adds a clock skew of 5 minutes by default, so a token lives that much longer than its expiry. Set ClockSkew to TimeSpan.Zero, keep ValidateLifetime and RequireExpirationTime true, and make sure the token you issue actually carries an exp value.

  • Applies to ASP.NET Core 8/9/10, Microsoft.IdentityModel.Tokens 8.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Decode the token's exp claim and compare it with the current time, then read your TokenValidationParameters.

    date -d @$(echo "$TOKEN" | cut -d. -f2 | base64 -d 2>/dev/null | jq .exp)
    date
  2. 2Match what you see, then jump to its fix

    1. 1The token is accepted for about 5 minutes after exp, then refusedThe default clock skew of 5 minutesFix
    2. 2ValidateLifetime = false in the optionsLifetime validation is switched offFix
    3. 3Decoded payload has no exp claimThe token has no exp, and RequireExpirationTime is falseFix

What does the error look like?

  • Token expired minutes ago but the API still returns 200

Cause 1 of 3

The default clock skew of 5 minutes

1Check

DefaultClockSkew is 300 seconds (5 minutes), and ClockSkew is added to time checks. If you never set it, you get that grace period.

grep -rn "ClockSkew" --include=*.cs .

2Fix

Set ClockSkew to zero if you want exp to be exact. Keep servers on synchronised time.

builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddJwtBearer(o =>
    {
        o.TokenValidationParameters = new TokenValidationParameters
        {
            ClockSkew = TimeSpan.Zero
        };
    });

3Verify

Call the API with a token whose exp passed a minute ago; it should return 401.

curl -i -H "Authorization: Bearer $OLD_TOKEN" https://localhost:5001/api/me

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

Lifetime validation is switched off

1Check

ValidateLifetime controls whether the token lifetime is validated at all.

grep -rn "ValidateLifetime\|LifetimeValidator" --include=*.cs .

2Fix

Remove the override, or set ValidateLifetime = true. A custom LifetimeValidator that always returns true has the same effect, so remove it too.

o.TokenValidationParameters = new TokenValidationParameters
{
    ValidateLifetime = true,
    ClockSkew = TimeSpan.Zero
};

3Verify

An expired token now returns 401.

curl -i -H "Authorization: Bearer $OLD_TOKEN" https://localhost:5001/api/me

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The token has no exp, and RequireExpirationTime is false

1Check

RequireExpirationTime defaults to true, so a token without exp is normally rejected. A token that never expires is accepted only when this was set to false.

grep -rn "RequireExpirationTime" --include=*.cs .

2Fix

Set it back to true and make the token issuer include an exp claim.

o.TokenValidationParameters = new TokenValidationParameters
{
    RequireExpirationTime = true,
    ValidateLifetime = true
};

3Verify

A token without exp returns 401; a new login token with exp works.

curl -i -H "Authorization: Bearer $TOKEN_WITHOUT_EXP" https://localhost:5001/api/me

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep access tokens short-lived and use refresh tokens for longer sessions.
  • Validate signature, issuer, audience and expiry, as the ASP.NET Core JWT bearer guide advises.
  • Test expiry with a token you issued with a past exp date.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.