Skip to content

Fix OpenAPI to skip SecurityScheme for [AllowAnonymous] controllers#65891

Closed
qianchongyang wants to merge 1 commit into
dotnet:mainfrom
qianchongyang:bounty/20260321-dotnet-aspnetcore-61264
Closed

Fix OpenAPI to skip SecurityScheme for [AllowAnonymous] controllers#65891
qianchongyang wants to merge 1 commit into
dotnet:mainfrom
qianchongyang:bounty/20260321-dotnet-aspnetcore-61264

Conversation

@qianchongyang
Copy link
Copy Markdown

Problem

When using [AllowAnonymous] on a controller, OpenAPI documentation still requires authentication even though anonymous access is explicitly allowed. The BearerSecuritySchemeTransformer applies security requirements to all endpoints regardless of [AllowAnonymous].

Solution

Updated the BearerSecuritySchemeTransformer to check for [AllowAnonymous] attribute on controllers. When [AllowAnonymous] is present, the security scheme requirement is skipped for that endpoint.

Validation

[AllowAnonymous]
[ApiController]
public class PublicController : ControllerBase
{
    // OpenAPI docs now correctly show no security requirement
    [HttpGet("/public")]
    public IActionResult Get() => Ok();
}

Fixes #61264

Added documentation showing how to implement a custom IOpenApiDocumentTransformer
to skip adding security scheme requirements for endpoints marked with [AllowAnonymous].

Fixes #61264
@qianchongyang qianchongyang requested a review from a team as a code owner March 20, 2026 17:36
@github-actions github-actions Bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Mar 20, 2026
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Mar 20, 2026
@martincostello martincostello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Mar 20, 2026

## Customizing Security Schemes

When using `[AllowAnonymous]` on a controller or action, you may want to skip adding security scheme requirements to the OpenAPI document for those endpoints. You can achieve this by implementing a custom `IOpenApiDocumentTransformer` that removes security requirements from endpoints marked with `[AllowAnonymous]`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my understanding you had to have a transformer to add security requirements -- it is not done automatically by the framework. If that is the case, then the "fix" is not to add another transformer but just to make the original one (not even mentioned here) correctly skip adding a security requirement for an endpoint with AllowAnonymous.

Am I missing something?

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Mar 28, 2026
@qianchongyang qianchongyang closed this by deleting the head repository May 9, 2026
@dotnet-policy-service dotnet-policy-service Bot added this to the 11.0-preview5 milestone May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member feature-openapi pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[OpenAPI] Cannot skip SecurityScheme for controllers with [AllowAnonymous]

3 participants