What we are wanting to achieve?
We have recently migrated our .NET project to Swashbuckle.AspNetCore 10.1.7 from version 6.6.2 as part of a larger .NET 8 to .NET 10 migration. However, I'm experiencing serious authorization problems with the new version of Swagger UI.
Previous Configuration (Pre-Migration)
Before the migration, we had this authentication/authorization setup:
1. OAuth2 Implicit Authorization Scheme
We used an implicit flow to log in and receive the access token.
Authority/Identity Provider Configuration:
public static AuthenticationBuilder AddAuthenticationForClientPortalApi(
this IServiceCollection services,
AuthorityConfig authorityConfig,
Action<IdentityServerAuthenticationOptions> configureOptions = null)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
return services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = authorityConfig.BaseUrl;
options.ApiName = authorityConfig.ApiName;
configureOptions?.Invoke(options);
});
}
Swagger Security Definition:
We added the oauth2 security definition to receive an access token by submitting credentials on the identity provider's login page.
return services.AddTransient<IConfigureOptions<SwaggerGenOptions>, TSwaggerConfig>()
.AddSwaggerGen(options =>
{
options.DescribeAllParametersInCamelCase();
options.OperationFilter<SwaggerDefaultValuesFilter>();
options.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Flows = authFlow == AuthFlowType.Implicit ?
new OpenApiOAuthFlows()
{
Implicit = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri(authorityConfig?.AuthorizationUrl),
Scopes = scopes
}
} :
new OpenApiOAuthFlows()
{
ClientCredentials = new OpenApiOAuthFlow()
{
TokenUrl = new Uri(authorityConfig?.AuthorizationUrl),
Scopes = scopes
}
}
});
options.OperationFilter<TAuthFilter>();
options.EnableAnnotations();
if (maskSchemas)
{
options.CustomSchemaIds(type =>
Convert.ToBase64String(Encoding.UTF8.GetBytes(type?.FullName.ToSha256() ?? ""))
.Trim('='));
}
});
2. Swagger UI Login Flow
This configuration allowed for login using Swagger UI's Authorize button and an implicit redirect to the login page of the identity provider.
3. Request Execution
Executing authorized endpoint requests automatically inserted the Authorization header with the Bearer access token.
Post-Migration Issues (.NET 10 / Swashbuckle 10.1.7)
After migrating to the latest Swashbuckle.AspNetCore packages (with only namespace adjustments), the behavior has changed at step 3:
- Step 1: No changes, works correctly.
- Step 2: No changes, works correctly.
- Step 3 (The Issue): When I execute an arbitrary API request using the Swagger UI, a cookie is sent to the API but the
Authorization header with a Bearer token is missing.
Questions
- How can I configure Swagger UI in version 10.x.x to send the received Bearer tokens in an
Authorization header alongside the cookie when executing API endpoint calls, while maintaining the initial OAuth2 implicit flow login?
- Where does Swagger UI store its authorization data after the initial login/unlock of the API page in version 10.x.x?
!NOTE
Sorry, if this was already answered. I didn't manage to find a solution to the problem that targets version 10.. , and the suggestions for previous versions didn't work for me.
What we are wanting to achieve?
We have recently migrated our .NET project to
Swashbuckle.AspNetCore10.1.7 from version 6.6.2 as part of a larger .NET 8 to .NET 10 migration. However, I'm experiencing serious authorization problems with the new version of Swagger UI.Previous Configuration (Pre-Migration)
Before the migration, we had this authentication/authorization setup:
1. OAuth2 Implicit Authorization Scheme
We used an implicit flow to log in and receive the access token.
Authority/Identity Provider Configuration:
Swagger Security Definition:
We added the
oauth2security definition to receive an access token by submitting credentials on the identity provider's login page.2. Swagger UI Login Flow
This configuration allowed for login using Swagger UI's Authorize button and an implicit redirect to the login page of the identity provider.
3. Request Execution
Executing authorized endpoint requests automatically inserted the
Authorizationheader with theBeareraccess token.Post-Migration Issues (.NET 10 / Swashbuckle 10.1.7)
After migrating to the latest
Swashbuckle.AspNetCorepackages (with only namespace adjustments), the behavior has changed at step 3:Authorizationheader with aBearertoken is missing.Questions
Authorizationheader alongside the cookie when executing API endpoint calls, while maintaining the initial OAuth2 implicit flow login?