English 中文(简体)
“Filter For Swagger不适当工作”行动显示,每个小点都有授权选择
原标题:OperationFilter For Swagger not working properly - it shows authorize option on every api endpoint

I am creating an api with .net6.0. I wanted to add Swagger UI for the api document , which I added and it works for the most part. The only problem I am facing is swagger UI is showing padlock on every endpoint there exists.

Below is my code - Program.cs

    builder.Services.AddSwaggerGen(options =>
{
    options.SupportNonNullableReferenceTypes();
    options.OperationFilter<MyApi.API.Filters.SecurityRequirementsOperationFilter>();
    options.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
    options.SwaggerDoc("v1", new OpenApiInfo() { Title = "MyAPI API", Version = "v1" });
    options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
    {
        In = ParameterLocation.Header,
        Description = "Please enter a valid token",
        Name = "Authorization",
        Type = SecuritySchemeType.Http,
        BearerFormat = "JWT",
        Scheme = "Bearer"
    });
    options.AddSecurityRequirement(new OpenApiSecurityRequirement()
    {
        {
            new OpenApiSecurityScheme()
            {
                Reference=new OpenApiReference()
                {
                    Type=ReferenceType.SecurityScheme,
                    Id="Bearer"
                }
            },
            new string[]{}
        }
    });
    options.IncludeXmlComments(xmlPath);
});

Here as you can see I am using operation filter to mark the authorize options.

安全要求 (我的行动过滤器)

public class SecurityRequirementsOperationFilter : IOperationFilter
    {
        public void Apply(OpenApiOperation operation, OperationFilterContext context)
        {
            // Policy names map to scopes
            var requiredScopes = context.MethodInfo
                .GetCustomAttributes(true)
                .OfType<AuthorizeAttribute>()
                .Select(attr => attr.Policy)
                .Distinct();

            if (requiredScopes.Any())
            {

                operation.Responses.Add("401", new OpenApiResponse { Description = "Unauthorised" });
                operation.Responses.Add("403", new OpenApiResponse { Description = "Forbidden" });

                operation.Security = new List<OpenApiSecurityRequirement>();
                var secReq = new List<OpenApiSecurityRequirement>();

                operation.Security.Add(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Description="Adds token to header",
                            Name="Authorization",
                            Type=SecuritySchemeType.Http,
                            In=ParameterLocation.Header,
                            Scheme = JwtBearerDefaults.AuthenticationScheme,
                            Reference = new OpenApiReference
                            {
                                Type= ReferenceType.SecurityScheme,
                                Id=JwtBearerDefaults.AuthenticationScheme
                            }
                        },new List<string>()
                    }
                });
            }
        }
    }

After these configuration I am doing normal CRUD operations in my controllers which are protected and require the JWT Bearer and hence I have decorated these end point with [Athorize] attribute,but in addition I also have two endpoints - /login and /register which obviously I want keep them as publicly available so I decorated them with [AllowAnonymus] attribute. Problem here is generated swagger UI shows padlock on every endpoint like below -

“entertext

我如何能够在我公开的终点上清除这些路障? 我在这里没有哪一个组合?

预支

问题回答

你们已经看到了这一问题,因为大约6万岁。 无论如何,你不需要补充。

options.AddSecurityRequirement(new OpenApiSecurityRequirement()
    {
        {
            new OpenApiSecurityScheme()
            {
                Reference=new OpenApiReference()
                {
                    Type=ReferenceType.SecurityScheme,
                    Id="Bearer"
                }
            },
            new string[]{}
        }
    });

页: 1 安全要求:Filter.cs过滤





相关问题
Random "Timed out ProduceRequest in flight" messages

I am getting some random timeout errors while publishing messages using Confluent.Kafka. The application runs in a Kubernetes cluster and is built using the .NET 6 framework. When the default timeout (...

如何将.Net 6.0的dll引用到.Net Framework 4.8中

大家好! 我有一个类库项目,目标是.NET Framework 6.0。当我将此DLL引用到目标为.NET Framework 4.8的另一个项目中时,我会收到以下错误消息 ...

热门标签