wasAsked.

JWT Authentication and Validation Flow

EPAM · Role not specified · technical

Published

Year not specified · Authentication & Security

Community answers

Posted by wasAsked editorial collection

The JWT flow has two parts: issuing the token after login, and validating it on every API request. Authentication (issuing) 1. The user submits credentials to the login endpoint (or signs in at an identity provider via OpenID Connect). 2. The server verifies them — password hash comparison, MFA, account status. 3. The server builds claims (sub, email, roles, jti), sets iss, aud, iat and exp, and signs the token with its private key (RS256) or shared secret (HS256). 4. It returns an access token (short-lived) and a refresh token (long-lived, stored server-side). Using the token 5. The client stores it securely and sends it with each request: Authorization: Bearer <token>. Validation (ASP.NET Core JwtBearer handler) 6. Parse the token into header, payload and signature; reject malformed tokens. 7. Check the algorithm is one you expect (reject "none" and unexpected algorithms). 8. Verify the signature with the correct key — for RS256, the issuer's public key from its JWKS endpoint, selected by the kid header. Any change to the payload breaks the signature. 9. Validate claims: - iss equals the trusted issuer, - aud equals this API, - exp has not passed and nbf has been reached (with small clock skew). 10. On success, create a ClaimsPrincipal (HttpContext.User). On failure, return 401. Authorization 11. Policies check roles, scopes or claims ([Authorize(Policy = "Admin")]), and resource-based checks confirm access to specific records. Configuration example: builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(o => { o.Authority = "https://login.example.com"; // discovers keys automatically o.Audience = "orders-api"; o.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(1); }); Renewal and revocation 12. When the access token expires, the client uses the refresh token to get a new one. Logout revokes the refresh token; access tokens expire naturally. Security essentials: HTTPS only, short access-token lifetimes, no sensitive data in the payload, protected signing keys with rotation, and never logging tokens.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics