All articles

JWT and refresh tokens: validation, rotation and browser sessions

JWT claims and signature validation, HttpOnly cookies, hashed refresh tokens, rotation races, shared refresh work and cross-tab coordination.

t.Turgay
JWT and refresh tokens: validation, rotation and browser sessions

Using JWT does not move every session decision into a token. Access-token validation, single-use refresh rotation and the browser's handling of obsolete responses need one coherent design. This guide focuses on HS256, .NET validation parameters, cookie policy and asynchronous coordination on the Angular side.

JWT structure and its trust boundary

A common signed compact JWT has Base64url header, payload and signature segments. Base64url encoding does not hide claims. Passwords, long-lived secrets and server-only data therefore do not belong in the readable payload.

iss identifies the issuer, aud the intended audience, exp expiry and nbf the earliest valid use. Decoding only reads those fields. Trust requires signature verification, an allowed algorithm and validation of the claim boundaries.

RFC 7519 — JSON Web Token

HS256 and validation parameters

HS256 uses a shared secret for HMAC-SHA256 signing and verification. The server selects the permitted algorithm rather than trusting the token's alg value as policy. kid may select among already trusted keys; it should not create an unrestricted source of new trusted keys or URLs.

The fragment below expresses a validation policy. The signing key and expected issuer and audience come from trusted configuration. ClockSkew is an explicit time tolerance, not an invisible extension to ignore. Endpoints may still require current account authorization after token validation.

var validation = new TokenValidationParameters {
    ValidateIssuerSigningKey = true,
    IssuerSigningKey = signingKey,
    ValidAlgorithms = [SecurityAlgorithms.HmacSha256],
    ValidateIssuer = true, ValidIssuer = expectedIssuer,
    ValidateAudience = true, ValidAudience = expectedAudience,
    ValidateLifetime = true, RequireExpirationTime = true,
    RequireSignedTokens = true,
    ClockSkew = TimeSpan.FromSeconds(30)
};

Microsoft Learn — SignalR authentication

In-memory access tokens and an HttpOnly refresh cookie

A short-lived access token can stay in memory, with refresh rebuilding the session after reload. An HttpOnly refresh cookie prevents direct JavaScript access to its value. Secure requires HTTPS, while SameSite and Path affect which browser requests carry it.

HttpOnly does not stop injected script from making authorized requests. Cookie-based refresh and logout still need CSRF defenses. A custom-header defense relies on a restrictive credentialed CORS policy. Cookie deletion must also match the path used when setting it.

response.Cookies.Append("__Secure-session_refresh", refreshToken,
    new CookieOptions {
        HttpOnly = true, Secure = true,
        SameSite = SameSiteMode.Strict,
        Path = "/api/auth", MaxAge = refreshLifetime
    });

Refresh-token hashes and rotation

A refresh token is a high-entropy random value. Store a cryptographic hash instead of the plaintext token and hash the received value for lookup. This differs from user-password hashing: low-entropy passwords need a slow salted password-derivation scheme.

Rotation marks the old token used and inserts a replacement atomically. A family identifier supports revoking related tokens after detected reuse. Carrying the absolute family expiry into replacements prevents renewal from extending the session forever.

RFC 9700 — refresh-token security

Concurrent refresh and shared in-flight work

Several HTTP requests may observe 401 simultaneously. Independent refresh calls then race over a single-use token. A shared promise combines that work inside one tab and clears the slot afterward. Here performRefresh is assumed to update the access token through the intended session policy.

The refresh request must not recursively trigger its own authentication interceptor. Logout during refresh also needs protection: an obsolete response must not restore the token afterward. A generation counter or cancellation boundary can determine whether the result still belongs to the active session.

let inFlight: Promise<RefreshResult> | null = null;
function refreshOnce(): Promise<RefreshResult> {
  return inFlight ??= performRefresh().finally(() => {
    inFlight = null;
  });
}

Web Locks, BroadcastChannel and server decisions

Where available, Web Locks can serialize refresh work across same-origin tabs. BroadcastChannel can announce login and logout without distributing the refresh-token value. Server concurrency policy must remain correct when those APIs are unavailable.

Conditional writes, transactions and family revocation remain the authoritative decision boundary. Browser coordination reduces unnecessary races rather than replacing authorization. Temporary unavailability, expiry and revocation also need distinct outcomes so an outage is not misreported as a denied session.

Related technical articles

You have reached the end of this orbit.
Explore more articles
turgay.A little code, a few pixels, a lot of curiosity.© 2026 · Made on Earth