OAuth2 Authorization Code Flow: `client_id` not sent in token exchange, phantom duplicate scheme on scope selection
What happens?
Three issues with OAuth2 Authorization Code Flow + PKCE configuration in Scalar.AspNetCore 2.13.11 (also reproduced on 2.12.54):
1. client_id not included in token request body
When using AddAuthorizationCodeFlow with flow.ClientId, the client_id is correctly sent in the authorization redirect URL but is not included in the POST body to the token endpoint. This causes Azure AD (and any OAuth2 provider requiring client_id for public clients) to reject the token exchange with:
AADSTS900144: The request body must contain the following parameter: 'client_id'Workaround: Explicitly adding it via flow.AddBodyParameter("client_id", clientId).
2. Phantom duplicate OAuth2 scheme appears when a scope is selected
When a scope is selected (either manually in the UI or via flow.SelectedScopes), a second phantom "OAuth2" entry appears in the authentication dropdown. Deselecting the scope makes the phantom disappear. This occurs regardless of whether the scheme is defined in the OpenAPI document, in Scalar config, or both.
3. AddDefaultScopes and SelectedScopes do not pre-select scopes
When scopes are defined in the OpenAPI document's security scheme, neither flow.SelectedScopes nor AddDefaultScopes pre-selects them in the Scalar UI. The user must manually select scopes each time.
Configuration
// OpenAPI document transformer defines the scheme with scopes
services.AddOpenApi("v1", options =>
{
options.AddDocumentTransformer((document, context, _) =>
{
document.Components ??= new OpenApiComponents();
document.Components.SecuritySchemes ??= new Dictionary<string, IOpenApiSecurityScheme>();
document.Components.SecuritySchemes["OAuth2"] = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = new OpenApiOAuthFlows
{
AuthorizationCode = new OpenApiOAuthFlow
{
AuthorizationUrl = new Uri("https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize"),
TokenUrl = new Uri("https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"),
Scopes = new Dictionary<string, string>
{
["api://{client-id}/access_as_user"] = "Access API as user"
}
}
}
};
document.Security ??= [];
document.Security.Add(new OpenApiSecurityRequirement
{
[new OpenApiSecuritySchemeReference("OAuth2")] = ["api://{client-id}/access_as_user"]
});
return Task.CompletedTask;
});
});
// Scalar config sets client ID and PKCE
app.MapScalarApiReference(options =>
{
options.AddDocument("v1", "API v1");
options.AddAuthorizationCodeFlow("OAuth2", flow =>
{
flow.ClientId = "{client-id}";
flow.Pkce = Pkce.Sha256;
flow.SelectedScopes = ["api://{client-id}/access_as_user"];
// WORKAROUND: client_id not sent in token exchange without this
flow.AddBodyParameter("client_id", "{client-id}");
});
});What did you expect to happen?
client_idin token exchange:flow.ClientIdshould automatically includeclient_idin both the authorization redirect URL and the token endpoint POST body. This is required by the OAuth2 spec for public clients using PKCE (RFC 7636 Section 4.5, RFC 6749 Section 4.1.3).No phantom duplicate: Selecting a scope on an OAuth2 scheme should not create a second phantom entry in the authentication dropdown. There should always be exactly one entry per defined scheme.
Scopes pre-selected:
flow.SelectedScopesandAddDefaultScopesshould pre-select scopes in the UI when scopes are defined in the OpenAPI document's security scheme.
Environment
Scalar.AspNetCore2.13.11 (also reproduced on 2.12.54)- .NET 10.0
Microsoft.AspNetCore.OpenApi10.0.5- Azure AD (Entra ID) as OAuth2 provider
- Authorization Code Flow with PKCE (public client, no client secret)
OpenAPI Document
Source: scalar/scalar