Skip to content

Commit 232805a

Browse files
authored
Merge pull request #36907 from dotnet/main
Merge to Live
2 parents 5a22adb + b0aa907 commit 232805a

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

aspnetcore/fundamentals/openapi/customize-openapi.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to customize OpenAPI documents in an ASP.NET Core app
55
ms.author: wpickett
66
monikerRange: '>= aspnetcore-9.0'
77
ms.custom: mvc
8-
ms.date: 10/29/2025
8+
ms.date: 03/20/2026
99
uid: fundamentals/openapi/customize-openapi
1010
---
1111
# Customize OpenAPI documents
@@ -106,6 +106,52 @@ Operation transformers can also be added to specific endpoint with the <xref:Mic
106106

107107
[!code-csharp[](~/fundamentals/openapi/samples/10.x/WebMinOpenApi/Program.cs?name=snippet_operationtransformer2)]
108108

109+
### Conditionally applying security requirements
110+
111+
In some scenarios, developers may want to apply security requirements to all endpoints except those explicitly marked with the `AllowAnonymous` attribute.
112+
113+
Use an operation transformer, which has access to endpoint metadata through the associated <xref:Microsoft.AspNetCore.Mvc.ApiExplorer.ApiDescription>.
114+
115+
The following example demonstrates how to skip adding a security requirement for endpoints that have the `AllowAnonymousAttribute` applied:
116+
117+
```csharp
118+
internal sealed class AuthOperationTransformer : IOpenApiOperationTransformer
119+
{
120+
public Task TransformAsync(
121+
OpenApiOperation operation,
122+
OpenApiOperationTransformerContext context,
123+
CancellationToken cancellationToken)
124+
{
125+
var hasAllowAnonymous = context.Description.ActionDescriptor.EndpointMetadata
126+
.OfType<AllowAnonymousAttribute>()
127+
.Any();
128+
129+
if (hasAllowAnonymous)
130+
{
131+
return Task.CompletedTask;
132+
}
133+
134+
operation.Security ??= new List<OpenApiSecurityRequirement>();
135+
136+
operation.Security.Add(new OpenApiSecurityRequirement
137+
{
138+
[new OpenApiSecurityScheme
139+
{
140+
Reference = new OpenApiReference
141+
{
142+
Id = "Bearer",
143+
Type = ReferenceType.SecurityScheme
144+
}
145+
}] = Array.Empty<string>()
146+
});
147+
148+
return Task.CompletedTask;
149+
}
150+
}
151+
```
152+
153+
Use this approach instead of document transformers when conditional logic based on endpoint metadata is required. This transformer adds security *requirements* per operation and assumes the security *scheme* is already registered at the document level. For an example of registering the Bearer security scheme, see the `BearerSecuritySchemeTransformer` in the [Use document transformers](#use-document-transformers) section.
154+
109155
## Use schema transformers
110156

111157
Schemas are the data models that are used in request and response bodies in an OpenAPI document. Schema transformers are useful when a modification:

0 commit comments

Comments
 (0)