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

时间:2016-12-09 06:28:50

标签: c# asp.net-web-api swagger .net-core swagger-ui

如何在Asp .Net核心中的Swagger中添加基本授权标头。默认情况下,api键将其视为查询字符串,但我需要对其进行自定义以使其包含在标题中。

4 个答案:

答案 0 :(得分:3)

经过数小时的修补后,我找到了这个解决方案

首先实现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);
        }
    }

然后实现接口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; }
    }
  

这里非常重要的是Type属性,而不是   接口需要但它必须作为swagger-ui存在   需要它

最后将它连接到你的swashbuckle配置

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

希望它有所帮助;)

答案 1 :(得分:2)

基于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"
            });
    }
}

最后将它连接到你的swashbuckler配置

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

答案 2 :(得分:2)

To place in swagger-ui navbar您可以使用此代码:

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

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

它会将JWT Token作为授权标头添加到每个请求中。

答案 3 :(得分:1)

如果您使用的是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>()
                }
            });