first commit
This commit is contained in:
33
Complex.EndPoint/Utility/SwaggerOperationFilter.cs
Normal file
33
Complex.EndPoint/Utility/SwaggerOperationFilter.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
public class SwaggerOperationFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
var authAttributes = context.MethodInfo.DeclaringType.GetCustomAttributes(true)
|
||||
.Union(context.MethodInfo.GetCustomAttributes(true))
|
||||
.OfType<AuthorizeAttribute>();
|
||||
|
||||
if (authAttributes.Any())
|
||||
{
|
||||
var securityRequirement = new OpenApiSecurityRequirement()
|
||||
{
|
||||
{
|
||||
new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = "Bearer"
|
||||
}
|
||||
},
|
||||
Array.Empty<string>()
|
||||
}
|
||||
};
|
||||
operation.Security = new List<OpenApiSecurityRequirement> { securityRequirement };
|
||||
operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorized" });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user