Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

ASP.NET Core: Why Your API Returns 401 Even Though the Token Looks Valid

- 31.08.26 - ErcanOPAK

⚙️ 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.

— Backend Architect

Related posts:

ASP.NET Core: Why a Singleton Holding a Scoped DbContext Silently Corrupts Data Under Load

ASP.NET Core: Why a Singleton Holding a Scoped DbContext Silently Corrupts Data Under Load

Dependency Injection Causes Memory Bloat

Dependency Injection Causes Memory Bloat

ASP.NET Core “ValidateOnStart” — Stop Production Crashes at Runtime

ASP.NET Core “ValidateOnStart” — Stop Production Crashes at Runtime

Views: 11
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Visual Studio: Fix the XAML Designer That Crashes Every Time You Open a Specific File
Photoshop: Recover an Unsaved File After Photoshop Crashes Mid-Edit

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 868 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com