ASP.NET Core › Authentication

How would you implement sign-out that properly invalidates both a cookie session and any associated server-side state?

Advanced

Answer

HttpContext.SignOutAsync(scheme) clears the authentication cookie on the client side, but for schemes needing genuine server-side invalidation (like revoking an active session token stored in a database or cache, important if you want immediate cross-device logout), you must also explicitly remove or mark that server-side record as invalid -- otherwise a copied/stolen cookie captured before sign-out could theoretically still be replayed if your validation doesn't check server-side revocation state.
public async Task<IActionResult> Logout() {
    var sessionId = User.FindFirst("sid")?.Value;
    await _sessionStore.RevokeAsync(sessionId);  // server-side invalidation
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);  // clears the cookie
    return RedirectToPage("/Index");
}
Real-world example A banking application's logout flow both clears the client cookie AND revokes the corresponding server-side session record, ensuring a captured cookie from before logout can't be replayed to regain access after the user explicitly signed out.

Common follow-ups: How would you validate server-side session revocation on every subsequent request without excessive database load?;What's the difference between this and simply relying on short cookie expiration?