简体   繁体   English

如何在ASP.Net核心的Swagger中添加基本授权标头

[英]How to add Basic authorization header in the Swagger in ASP .Net core

How to add basic authorization header in Swagger in Asp .Net core. 如何在Asp .Net核心的Swagger中添加基本授权标头。 By default the api key treats as a query string but I need to customize that so that it will include in the header. 默认情况下,api键被视为查询字符串,但我需要对其进行自定义,以便将其包含在标题中。

after hours of tinkering I found this solution 经过数小时的修补,我找到了这个解决方案

first implement the IOperationFilter as below: 首先实现IOperationFilter,如下所示:

public class AddRequiredHeaderParameter : IOperationFilter
    {
        void IOperationFilter.Apply(Operation operation, OperationFilterContext context)
        {
            var param = new Param();
            param.Name = "authorization";
            param.In = "header";
            param.Description = "JWT Token";
            param.Required = true;
            param.Type = "string";
            if (operation.Parameters == null)
                operation.Parameters = new List<IParameter>();
            operation.Parameters.Add(param);
        }
    }

then implement the interface IParameter 然后实现接口IParameter

class Param : IParameter
    {

        public string Description { get; set; }

        public Dictionary<string, object> Extensions { get {return new Dictionary<string, object>{{"test", true}};} }

        public string In { get; set; }

        public string Name { get; set; }

        public string Type { get; set; }

        public bool Required { get; set; }
    }

the VERY important thing here is the Type property which is not required by the interface but it has to be there as the swagger-ui will need it 这里非常重要的是接口不需要的Type属性,但是它必须存在,因为swagger-ui将需要它

and finally hook it up to your swashbuckle config 最后将其连接到您的Swashbuckle配置

services.ConfigureSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
    options.SingleApiVersion(new Info
    {
        Version = "v1",
        Title = "Test",
        Description = "Test Service",
        TermsOfService = "None"
    });
    options.DescribeAllEnumsAsStrings();
});

Hope it helps ;) 希望能帮助到你 ;)

Based on Mohsen's reply figured out we can do this without having to implement the Iparameter interface 根据Mohsen的答复,我们可以在无需实现Iparameter接口的情况下执行此操作

public class AddRequiredHeaderParameter : IOperationFilter
{
    void IOperationFilter.Apply(Operation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
            operation.Parameters = new List<IParameter>();
            operation.Parameters.Add(new NonBodyParameter
            {
                Name = "Authorization",
                In = "header",
                Description = "JWT Token",
                Required = true,
                Type = "string"
            });
    }
}

and finally hook it up to your swashbuckler config 最后将其连接到您的swashbuckler配置

    services.ConfigureSwaggerGen(options =>
{
    options.OperationFilter<AddRequiredHeaderParameter>();
    options.SingleApiVersion(new Info
    {
        Version = "v1",
        Title = "Test",
        Description = "Test Service",
        TermsOfService = "None"
    });
    options.DescribeAllEnumsAsStrings();
});

To place in swagger-ui navbar you can use this code: 要放置在swagger-ui导航栏中,您可以使用以下代码:

services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                ...
            });

            c.AddSecurityDefinition("JWT Token", new ApiKeyScheme
            {
                Description = "JWT Token",
                Name = "Authorization",
                In = "header"
            });
        });

It will add JWT Token as Authorization header to every request. 它将向每个请求添加JWT令牌作为Authorization标头。

If you are using Swashbuckle.AspNetCore v5.0.0-rc2 see below: 如果您使用的是Swashbuckle.AspNetCore v5.0.0-rc2,请参见以下内容:

   c.AddSecurityDefinition("Basic", new OpenApiSecurityScheme
            {
                Description = "Basic auth added to authorization header",
                Name = "Authorization",
                In = ParameterLocation.Header,
                Scheme = "basic",
                Type = SecuritySchemeType.Http
            });

            c.AddSecurityRequirement(new OpenApiSecurityRequirement
            {
                {
                    new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Basic" }
                    },
                    new List<string>()
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM