在ASP.net核心Web API(Swashbuckle.AspNetCore-Version =“ 5.0.0-rc4”)

时间:2019-12-15 15:33:40

标签: .net asp.net-core swagger-ui

我想添加自定义标头来招摇。我正在使用swagger 5.0.0-rc4的asp.net core 3.0。 我的中间件会收到类似于“密钥:价值”的东西。

public class MyHeaderFilter : IOperationFilter
{
    public void Apply(OpenApiOperation operation, OperationFilterContext context)
    {  
        if (operation.Parameters == null)
                operation.Parameters = new List<OpenApiParameter>();

            operation.Parameters.Add(new OpenApiParameter
            {
                Name = "Authorization",
                In = ParameterLocation.Header,
                Description = "Authenticate API Key",
                Required = false,

                Schema = new OpenApiSchema
                {
                    Type = "String",
                    Default = new OpenApiString("Bearer")
                }
            });

    }
}

1 个答案:

答案 0 :(得分:0)

您应该在Startup.cs中注册:

// Startup.cs
services.AddSwaggerGen(c =>
{
    ...
    c.OperationFilter<AuthResponsesOperationFilter>();
};

此外,似乎您正在根据thisthis线程传递Authorization标头:

  

将不会使用/ auth / credentials和/ system / info中定义的Authorization标头参数,因为OpenAPI规范指出工具应忽略名为Authorization的显式标头参数。应该将Authorization标头定义为安全方案。