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.
Likely 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?
IClaimsTransformation.TransformAsync is called automatically after authentication succeeds, letting you add additional claims not present in the original token/cookie -- such as fetching a user's current roles or permissions from a database that may change more frequently than the token itself, avoiding the need to reissue a new token every time such data changes.
public class RolesClaimsTransformer : IClaimsTransformation {
public async Task<ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) {
var identity = (ClaimsIdentity)principal.Identity;
if (!identity.HasClaim(c => c.Type == "enriched")) {
var roles = await _roleService.GetRolesAsync(principal.FindFirst("sub").Value);
foreach (var role in roles) identity.AddClaim(new Claim(ClaimTypes.Role, role));
identity.AddClaim(new Claim("enriched", "true"));
}
return principal;
}
}
Real-world example
An application whose user roles can change mid-session (an admin revokes a permission) uses claims transformation to fetch current roles from the database on every request, rather than relying on potentially stale roles baked into a long-lived JWT.
Likely follow-ups
What's the performance cost of a database call on every request via claims transformation, and how would you cache it?
How does this differ from simply putting more claims directly in the original token?
Windows/Negotiate authentication (via the Microsoft.AspNetCore.Authentication.Negotiate package) leverages the client machine's existing domain-joined Windows credentials, transparently authenticating the user without any login prompt when both client and server are on the same Active Directory domain -- appropriate specifically for internal intranet line-of-business applications where all users are on managed, domain-joined corporate machines, but entirely unsuitable for public-facing or external applications.
An internal HR portal accessible only from within the corporate network uses Windows Authentication so employees are automatically signed in using their existing domain credentials, with zero separate login screen or password to manage.
Likely follow-ups
Why is Windows Authentication inappropriate for internet-facing applications?
How does this interact with load balancers or reverse proxies in front of the application?
SignInAsync/SignOutAsync are imperative actions that actually establish or clear an authenticated session (writing/clearing a cookie, or in the case of custom schemes, whatever persistence mechanism the scheme uses) -- distinct from HttpContext.User.Identity.IsAuthenticated, which is merely a read-only check of the current request's already-established identity state, not an action that changes anything.
// Action: actually establishes a new authenticated session
await HttpContext.SignInAsync(scheme, principal);
// Query: just reads the CURRENT request's already-established state
bool isLoggedIn = HttpContext.User.Identity?.IsAuthenticated ?? false;
Real-world example
A login action handler calls SignInAsync once after validating credentials to establish the session, while every other part of the application simply reads User.Identity.IsAuthenticated to check the already-established state without triggering any new sign-in action.
Likely follow-ups
What actually happens internally when SignInAsync is called for cookie authentication?
Can you be signed in under multiple schemes simultaneously?
A magic link flow generates a short-lived, single-use, cryptographically random token associated with the user's email, stored server-side (database or cache) with an expiration, emailed as a URL parameter; when clicked, the endpoint validates the token (checking existence, expiration, and single-use consumption), and if valid, calls SignInAsync to establish an authenticated session -- effectively substituting 'possession of the emailed link' for a traditional password.
app.MapGet("/auth/magic-link", async (string token, IMagicLinkStore store, HttpContext ctx) => {
var email = await store.ValidateAndConsumeAsync(token); // single-use, expires quickly
if (email is null) return Results.Unauthorized();
var principal = BuildPrincipalForUser(email);
await ctx.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
return Results.Redirect("/dashboard");
});
Real-world example
A B2C SaaS product offers passwordless login via emailed magic links to reduce signup friction and eliminate password-related support tickets, using a database-backed single-use token store to ensure each link can only be used once.
Likely follow-ups
How do you prevent a magic link from being forwarded or intercepted and reused maliciously?
What's the UX trade-off of passwordless authentication for users without reliable email access?