#8503·scalar

OAuth2 授权码流: 在令牌交换中未发送 `client_id`, 在范围选择时出现虚拟重复方案

作者: edlrod创建于 2026年3月18日更新于 2026年9月25日
标签bug

// 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 = …