In ASP.NET Core, what do the default scheme settings on AddAuthentication control, and what happens when no default challenge scheme can be resolved?
answer
- one default per action
- DefaultScheme is the fallback
- forbid falls back to challenge
- single scheme becomes default automatically
- InvalidOperationException: no DefaultChallengeScheme found
basics
~20 sAuthenticationOptions 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.
solid answer
~40 s`AddAuthentication("Cookies")` sets `DefaultScheme`; the options overload also exposes `DefaultAuthenticateScheme`, `DefaultChallengeScheme`, `DefaultForbidScheme`, `DefaultSignInScheme` and `DefaultSignOutScheme`. Each action uses its own setting first, then `DefaultScheme`; forbid falls back to the challenge default and sign-out to the sign-in default. The authentication middleware only runs the **default authenticate** scheme to fill `HttpContext.User`. Since ASP.NET Core 7, if exactly one scheme is registered it is used as the default automatically; with two schemes and no default, `User` stays anonymous and the first `[Authorize]` challenge throws `InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found`. The fix is a default or naming the scheme on the endpoint.
code
csharp · 13 linesbuilder.Services
.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options => options.LoginPath = "/Login")
.AddScheme<AuthenticationSchemeOptions, PartnerKeyHandler>("PartnerKey", null);
app.MapGet("/partner/feed", (FeedService feed) => feed.Latest())
.RequireAuthorization(policy => policy
.AddAuthenticationSchemes("PartnerKey")
.RequireAuthenticatedUser());go deeper
Remember that a scheme is a named handler and that AddAuthentication("Cookies") makes Cookies the default for everything unless you set something more specific.
Explain the per-action defaults and their fallbacks, the single-scheme automatic default, and why only the default authenticate scheme fills User.
Diagnose the 'no DefaultChallengeScheme found' exception after a second scheme lands, and choose between explicit defaults, per-endpoint scheme names and a policy scheme.
Decide how an app with browser users, partners and internal services should split schemes so each caller gets the right challenge without coupling every endpoint to scheme names.
## One default per action Every authentication operation in ASP.NET Core goes through `IAuthenticationService`, and every operation can be called with or without a scheme name. When the name is omitted, the service asks `IAuthenticationSchemeProvider` for the **default for that action**, configured on `AuthenticationOptions`: | Action | Option read first | Falls back to | |---|---|---| | `AuthenticateAsync` | `DefaultAuthenticateScheme` | `DefaultScheme` | | `ChallengeAsync` | `DefaultChallengeScheme` | `DefaultScheme` | | `ForbidAsync` | `DefaultForbidScheme` | the challenge default | | `SignInAsync` | `DefaultSignInScheme` | `DefaultScheme` | | `SignOutAsync` | `DefaultSignOutScheme` | the sign-in default | `AddAuthentication(string defaultScheme)` is shorthand for setting `DefaultScheme` alone; `AddAuthentication(Action<AuthenticationOptions>)` sets any of them. ## The automatic default Since ASP.NET Core 7, the scheme provider uses the **only** registered scheme as the default when `DefaultScheme` is not set. A portal with just `AddCookie()` therefore works with a bare `AddAuthentication()`. The behaviour can be turned off with the `Microsoft.AspNetCore.Authentication.SuppressAutoDefaultScheme` app-context switch. The moment a second scheme is added (an API-key scheme for a partner feed, say) the automatic default disappears, and code that relied on it silently changes behaviour. ## What the middleware actually does The authentication middleware is small: 1. It gives handlers that implement `IAuthenticationRequestHandler` (remote schemes such as OpenID Connect callbacks) a chance to handle the request. 2. It resolves the **default authenticate scheme**; if there is one, it calls `AuthenticateAsync` and assigns a returned principal to `HttpContext.User`. 3. It calls the next middleware, whatever the result. So: - Only **one** scheme populates `User` automatically. A second scheme is evaluated only when something asks for it by name, for example `[Authorize(AuthenticationSchemes = "PartnerKey")]` or a policy that lists schemes. - With **no** default authenticate scheme, the middleware authenticates nothing; every request looks anonymous. - The middleware never rejects a request. Rejection comes from authorization, which challenges (401 or a login redirect) or forbids (403). ## The exception everyone meets once When authorization challenges and no scheme can be resolved, `AuthenticationService.ChallengeAsync` throws: - `InvalidOperationException: No authenticationScheme was specified, and there was no DefaultChallengeScheme found. The default schemes can be set using either AddAuthentication(string defaultScheme) or AddAuthentication(Action<AuthenticationOptions> configureOptions).` The same message exists for authenticate, forbid, sign-in and sign-out. The typical cause in an interview scenario is a second `AddScheme` call added to an app that previously had one scheme and no explicit default. The fixes are: - set `DefaultScheme` (or the specific default that is missing); - name the scheme on the endpoint or in the policy; - put a **policy scheme** (`AddPolicyScheme`) in front that forwards to the right scheme per request. ## Splitting defaults deliberately Setting different defaults per action is common with remote schemes. An OpenID Connect web app sets `DefaultScheme` to the cookie scheme (so requests authenticate from the cookie) and `DefaultChallengeScheme` to the OIDC scheme (so a challenge goes to the identity provider). In a cookie-plus-API-key app, a frequent pattern is a cookie `DefaultScheme` for the portal and an explicit scheme name on the partner endpoints, so the partner feed never sees a login redirect. ## Checklist when sign-in misbehaves - Count the registered schemes; one means an automatic default, two or more means you must choose. - Check which scheme is the default **authenticate** scheme; that is the only one filling `User`. - Check that `SignInAsync` goes to a scheme that can sign in. A JWT bearer scheme cannot, and calling it throws.
- An app sets DefaultScheme to cookies and DefaultChallengeScheme to an OpenID Connect scheme. Which scheme handles a ForbidAsync call with no scheme name?Forbid falls back to the challenge default when `DefaultForbidScheme` is unset, so the OpenID Connect scheme receives it. Remote handlers usually delegate forbid to their sign-in scheme, but naming `DefaultForbidScheme` explicitly makes the behaviour obvious and avoids surprises.
- Why does a request carrying a valid partner key still show an anonymous HttpContext.User inside middleware that runs before authorization?The authentication middleware only runs the default authenticate scheme. If the key scheme is not that default, it is evaluated later, when the endpoint's policy names it, and that principal is used for the authorization decision. Middleware that needs it earlier must call `AuthenticateAsync("PartnerKey")` itself or use a policy scheme as the default.
- The authentication middleware tries every registered scheme and keeps the first that succeeds.
- You must always set DefaultScheme, even when only one scheme is registered.
- Authentication middleware returns 401 itself when no scheme authenticates the request.
- DefaultForbidScheme falls back to DefaultSignInScheme when it is not set.
- Adding a second AddScheme call leaves the original automatic default in place.
Assumes ASP.NET Core 10; the automatic default for a single registered scheme has existed since ASP.NET Core 7, so older answers that say a default is always required are out of date.