An expired JWT is still accepted by an ASP.NET Core API
Token expired minutes ago but the API still returns 200Short 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.
Fix it fast
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) date2Match what you see, then jump to its fix
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/meConfirmed by learn.microsoft.com
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/meConfirmed by learn.microsoft.com
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/meConfirmed by learn.microsoft.com
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
Airflow SSHOperator stops a long script with SSH command timed out
airflow.exceptions.AirflowException: SSH command timed out
SSHOperator waits only 10 seconds for a command by default.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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