add some filter to see which endpoints require authorization and which not
This commit is contained in:
parent
d8968ce557
commit
bfcba0beb7
2 changed files with 39 additions and 14 deletions
|
|
@ -0,0 +1,36 @@
|
|||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
|
||||
namespace SurveyBackend.Filters;
|
||||
|
||||
public class EndpointAuthRequirementFilter : IOperationFilter
|
||||
{
|
||||
public void Apply(OpenApiOperation operation, OperationFilterContext context)
|
||||
{
|
||||
if (!context.ApiDescription
|
||||
.ActionDescriptor
|
||||
.EndpointMetadata
|
||||
.OfType<AuthorizeAttribute>()
|
||||
.Any())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
operation.Security = new List<OpenApiSecurityRequirement>
|
||||
{
|
||||
new OpenApiSecurityRequirement
|
||||
{
|
||||
[new OpenApiSecurityScheme
|
||||
{
|
||||
Reference = new OpenApiReference
|
||||
{
|
||||
Type = ReferenceType.SecurityScheme,
|
||||
Id = JwtBearerDefaults.AuthenticationScheme
|
||||
}
|
||||
}] = new List<string>()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue