Advertisement
Advanced ASP.NET Core, Authentication

How do refresh tokens work alongside short-lived JWT access tokens, and how would you implement a refresh endpoint?

Short answer

Short-lived access tokens (minutes) limit the exposure window if stolen, while a longer-lived, securely-stored refresh token (often in an HttpOnly cookie) can be exchanged via a dedicated /auth/refresh endpoint for a new access token pair without requiring the user to re-enter…

Save ASP.NET Core mock interview
Advertisement
AModel answer
Short-lived access tokens (minutes) limit the exposure window if stolen, while a longer-lived, securely-stored refresh token (often in an HttpOnly cookie) can be exchanged via a dedicated /auth/refresh endpoint for a new access token pair without requiring the user to re-enter credentials -- the refresh endpoint validates the refresh token (checking it hasn't been revoked or expired), issues a new access token (and typically rotates the refresh token itself for added security), and returns both to the client.
app.MapPost("/auth/refresh", async (RefreshRequest request, ITokenService tokenService) => {
    var principal = tokenService.ValidateRefreshToken(request.RefreshToken);
    if (principal is null) return Results.Unauthorized();
    var newAccessToken = tokenService.GenerateAccessToken(principal);
    var newRefreshToken = tokenService.RotateRefreshToken(request.RefreshToken);
    return Results.Ok(new { accessToken = newAccessToken, refreshToken = newRefreshToken });
});
Real-world example
A mobile banking app uses 15-minute access tokens with a 30-day refresh token, silently refreshing the access token in the background so users stay logged in for a month without frequent re-authentication, while limiting a stolen access token's usefulness.
Likely follow-ups
  • What is refresh token rotation and why does it improve security over static refresh tokens?
  • How do you revoke a refresh token immediately during a security incident?
Related topics