skip to content

Schemes & Cookie Handler

AddAuthentication registers schemes whose handlers turn a cookie or header into HttpContext.User. Interviewers probe default schemes, cookie sign-in, and when a challenge redirects or returns 401.

part ofASP.NET Coreread it →
on this pageshow

explore

questions

6

In ASP.NET Core, what do the default scheme settings on AddAuthentication control, and what happens when no default challenge scheme can be resolved?

level: middleimportance: must knowfreq 60%

basics

~20 s

AuthenticationOptions holds a default scheme for each action (authenticate, challenge, forbid, sign-in, sign-out) with DefaultScheme as the fallback. If none resolves and nothing names a scheme, the call throws an InvalidOperationException saying no default scheme was found.

open as a page

In ASP.NET Core 10, why does an unauthenticated call to a cookie-protected [ApiController] action get 401 instead of a redirect to LoginPath, and how do you control it?

level: seniorimportance: should knowfreq 42%

basics

~10 s

Since ASP.NET Core 10, the cookie handler's default OnRedirectToLogin and OnRedirectToAccessDenied answer 401 or 403 instead of redirecting when the endpoint carries IDisableCookieRedirectMetadata, which [ApiController], JSON Minimal APIs, TypedResults and SignalR add automatically.

open as a page

In ASP.NET Core, when does IClaimsTransformation.TransformAsync run, and what goes wrong when it is used to add claims from a database?

level: seniorimportance: should knowfreq 40%

basics

~20 s

IClaimsTransformation.TransformAsync runs after every successful AuthenticateAsync, on every authenticated request and possibly several times per request. Its claims are never saved to the cookie, so it must be idempotent and cheap, or each request pays a database lookup.

open as a page

In ASP.NET Core, how would you let one app accept cookie sign-in for a parents' portal and an API key for a partner feed, using a custom handler and AddPolicyScheme?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Write an AuthenticationHandler that turns a valid key into an AuthenticateResult.Success ticket, register it with AddScheme next to AddCookie, and make an AddPolicyScheme the default whose ForwardDefaultSelector sends partner requests to the key scheme and everything else to cookies.

open as a page