⚙️ The Token Decodes Fine – The API Still Says Unauthorized
You paste the JWT into a decoder, every claim looks correct, the signature checks out, the token clearly hasn’t expired by the clock on your screen – and the API still returns 401 Unauthorized on every single request.
🐞 The Problem
HTTP/1.1 401 Unauthorized WWW-Authenticate: Bearer error="invalid_token", error_description="The token expired at ..." // But jwt.io shows "exp" is still 3 minutes in the future // according to your own system clock.
🔍 Why This Happens
JWT validation checks the "exp" claim against the SERVER's clock, not the client's. If the API server's system clock has drifted even a couple of minutes from real time (common on VMs, containers without NTP sync, or after a host migration), a token that's genuinely still valid by real-world time can be rejected as expired, or a token issued just a moment ago can be rejected as "not yet valid" (the "nbf" claim) - both purely because the two clocks disagree.
✅ The Fix: Check Clock Sync First, Then Add Tolerance
- Confirm the API server’s system time is actually correct and NTP-synced — this is the real root cause far more often than a genuine token bug, especially right after moving to a new host or container image.
- Add a small explicit clock skew tolerance to the JWT validation parameters so minor, expected drift doesn’t cause false rejections:
ClockSkew = TimeSpan.FromMinutes(2)(ASP.NET Core’s default is already 5 minutes, but a custom validator can accidentally override it to zero). - If the token is issued by a DIFFERENT server than the one validating it, make sure both are syncing to the same reliable time source — independent drift between issuer and validator is the classic cause here.
📦 Explicit Clock Skew in Token Validation
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateLifetime = true,
ClockSkew = TimeSpan.FromMinutes(2), // explicit, not accidentally zero
// ... issuer, audience, signing key, etc.
};
});
A token isn’t just data — it’s a claim about time, and two servers that disagree on what time it is will disagree on whether that claim still holds.




