.NET › Authentication & Authorization (Identity, JWT, OAuth)

How does token validation work under the hood when the JwtBearer middleware receives a request, and what checks does it perform?

Advanced

Answer

The middleware extracts the token from the Authorization header, verifies its cryptographic signature against the configured signing key (symmetric secret or asymmetric public key, often fetched dynamically from the issuer's JWKS endpoint), and validates standard claims: expiration (exp, rejecting expired tokens), not-before (nbf), issuer (iss, must match ValidIssuer), and audience (aud, must match ValidAudience) -- failing any check results in a 401 Unauthorized response before the request ever reaches your endpoint code.
options.TokenValidationParameters = new TokenValidationParameters {
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,
    ValidateLifetime = true,      // checks exp/nbf
    ValidateIssuer = true,
    ValidIssuer = "https://auth.myapp.com",
    ValidateAudience = true,
    ValidAudience = "myapp-api",
    ClockSkew = TimeSpan.FromMinutes(2)  // tolerance for clock drift
};
Real-world example A federated identity setup fetches signing keys dynamically from the identity provider's /.well-known/jwks.json endpoint, so the API can validate tokens without hardcoding a static key, automatically picking up key rotations.

Common follow-ups: What is JWKS and why does dynamic key fetching improve security over hardcoded keys?;Why is ClockSkew tolerance necessary between distributed systems?