skip to content

In ASP.NET Core 10's OpenIdConnectOptions, which defaults must change for the authorization code flow, and what do UsePkce and PushedAuthorizationBehavior then do?

level: seniorimportance: should knowfreq 45%

answer

  1. the default response type is not code
  2. ResponseType id_token, ResponseMode form_post
  3. UsePkce true, but only for code
  4. verifier rides in the protected state
  5. PushedAuthorizationBehavior.UseIfAvailable since 9

basics

~20 s

OpenIdConnectOptions.ResponseType defaults to id_token, so you set it to code and usually supply ClientSecret. PKCE then happens automatically, since UsePkce defaults to true, and since ASP.NET Core 9 the handler pushes the request via PAR whenever the provider advertises it.

solid answer

~50 s

`ResponseType` defaults to `OpenIdConnectResponseType.IdToken` with `ResponseMode = "form_post"`: the provider posts back an ID token only, with no access or refresh token, so `SaveTokens` keeps just the ID token and `GetClaimsFromUserInfoEndpoint` has nothing to call userinfo with. For the code flow set `ResponseType = OpenIdConnectResponseType.Code` and, for a confidential registration, `ClientSecret`, which the handler sends when it redeems the code. `UsePkce` is already `true` and applies only when the response type is `code`: the handler creates a random verifier, keeps it in the Data Protection-protected `state`, sends an S256 `code_challenge`, and adds `code_verifier` at redemption. `PushedAuthorizationBehavior` defaults to `UseIfAvailable` (new in 9): if discovery lists a PAR endpoint, the parameters go over the back channel and the browser carries only `client_id` and `request_uri`. `Require` and `Disable` throw when the provider cannot or must use PAR.

code

csharp · 17 lines
csharp
builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
    options.Authority = builder.Configuration["CorporateIdp:Authority"];
    options.ClientId = builder.Configuration["CorporateIdp:ClientId"];
    options.ClientSecret = builder.Configuration["CorporateIdp:ClientSecret"];

    options.ResponseType = OpenIdConnectResponseType.Code; // default is id_token
    // UsePkce is already true and now takes effect.
    options.PushedAuthorizationBehavior = PushedAuthorizationBehavior.Require;
    options.Scope.Add("email");
});

go deeper

for a junior

Recall that ASP.NET Core's OpenID Connect handler does not default to the code flow, and that ResponseType must be set to code.

for a middle

Explain which defaults change for the code flow, why the default yields no access token, and how the handler adds PKCE without extra code.

for a senior

Anticipate upgrade surprises such as PAR switching on by itself, form_post needing SameSite=None cookies over HTTPS, and silent no-ops from SaveTokens or userinfo on the id_token flow.

for a principal

Set a baseline client profile across the organisation's apps: code flow with PKCE, when to require PAR, and which scopes and token storage each app is allowed.

## The default is not the code flow `OpenIdConnectOptions` has kept defaults from the era when web apps asked for an ID token straight from the authorization endpoint: | Option | Default in ASP.NET Core 10 | What you usually want | |---|---|---| | `ResponseType` | `"id_token"` (`OpenIdConnectResponseType.IdToken`) | `"code"` (`OpenIdConnectResponseType.Code`) | | `ResponseMode` | `"form_post"` | leave as is | | `UsePkce` | `true` | leave as is | | `PushedAuthorizationBehavior` | `UseIfAvailable` | leave, or `Require` for high-assurance providers | | `Scope` | `openid`, `profile` | add `email`, `offline_access`, API scopes as needed | | `SaveTokens` | `false` | `true` if the app calls APIs or needs `id_token_hint` | | `GetClaimsFromUserInfoEndpoint` | `false` | `true` if profile claims live only at userinfo | With the default `id_token` response type the provider never issues an access token to the app. Two options then quietly do nothing useful: - `SaveTokens = true` stores only the `id_token`; - `GetClaimsFromUserInfoEndpoint = true` logs that no access token is available and skips the userinfo call. Setting `ResponseType = OpenIdConnectResponseType.Code` switches the handler to redeeming an authorization code at the token endpoint. `ClientSecret`, when set, is sent with that redemption; most providers require it for a confidential web registration. ## What `UsePkce` does for you `UsePkce` defaults to `true`, but the handler checks `UsePkce && ResponseType == "code"`, so it has **no effect** on the default response type. In the code flow, on every challenge the handler: 1. generates 32 random bytes and base64url-encodes them as the `code_verifier`; 2. stores the verifier in `AuthenticationProperties.Items`, which the handler protects with Data Protection into the `state` parameter, so no server-side store is involved; 3. sends `code_challenge` (the SHA-256 of the verifier) with `code_challenge_method=S256`; 4. on the callback, adds `code_verifier` to the token request and removes it from the properties. You do not write any of this. Turn `UsePkce` off only for a provider that rejects PKCE parameters. Note that `GoogleOptions` also sets `UsePkce = true`, while the base `OAuthOptions` defaults it to `false`. ## Response mode with the code flow The handler omits `response_mode` only for the pair `code` + `query`. With the default `form_post`, the provider returns the code in a cross-site **POST** to `/signin-oidc`. That is why the nonce and correlation cookies are written with `SameSite=None` and `Secure`: the browser must send them on that POST, so the app has to see HTTPS. ## Pushed authorization requests (PAR) ASP.NET Core 9 added PAR to `OpenIdConnectHandler`. Instead of putting every parameter in the browser redirect, the handler POSTs them, with the client credentials, to the provider's `pushed_authorization_request_endpoint` and receives a `request_uri`. The redirect then carries only `client_id` and `request_uri`, so parameters cannot be read or tampered with in the browser, and URLs stay short. `PushedAuthorizationBehavior` decides when this happens: - `UseIfAvailable` (**default**): push whenever the discovery document lists a PAR endpoint. An upgrade to 9 or 10 can therefore change traffic with no code change. - `Disable`: never push; throws `InvalidOperationException` if the discovery document says PAR is required. - `Require`: always push; throws if no PAR endpoint is available. `OpenIdConnectEvents.OnPushAuthorization` lets you change the pushed request, handle client authentication yourself (for example a signed client assertion), or skip or perform the push. ## Other parameters Extra authorization parameters such as `prompt` or an `audience` used to need `OnRedirectToIdentityProvider`. Since 9, `AdditionalAuthorizationParameters` adds them declaratively, and they are pushed along with the rest when PAR is used. ## Checklist for a code-flow client - `ResponseType = OpenIdConnectResponseType.Code`, `ClientSecret` from a secret store. - Leave `UsePkce` and `PushedAuthorizationBehavior` at their defaults unless the provider says otherwise. - Add `offline_access` to `Scope` only if the app will actually refresh tokens. - Serve the app over HTTPS so the `SameSite=None` correlation and nonce cookies survive the form post.

  • After an upgrade from ASP.NET Core 8 to 10, sign-in requests start going to a new provider endpoint before the redirect. Why?
    Since 9, `PushedAuthorizationBehavior` defaults to `UseIfAvailable`, so once the provider's discovery document lists a PAR endpoint the handler pushes the authorization parameters there first. Set `Disable` if the provider's PAR setup is broken, accepting that it throws when the provider requires PAR.
  • Where does the handler keep the PKCE code_verifier between the redirect and the callback?
    In `AuthenticationProperties.Items`, which travel inside the `state` parameter protected by Data Protection. On the callback the handler unprotects `state`, adds `code_verifier` to the token request and removes it, so no server-side store is needed.

  • OpenIdConnectOptions.ResponseType defaults to code, so the code flow needs no configuration.
  • UsePkce = true adds a code challenge whatever the response type is.
  • PAR is used only after you set PushedAuthorizationBehavior to Require.
  • With the default response type, GetClaimsFromUserInfoEndpoint fetches the userinfo claims.
  • The handler keeps the PKCE verifier in server memory, so it breaks behind a load balancer.

Assumes ASP.NET Core 10; PAR support in OpenIdConnectHandler, its UseIfAvailable default and AdditionalAuthorizationParameters arrived in ASP.NET Core 9.