如何在 ASP .Net 核心的 Swagger 中添加基本授权标头
Posted
技术标签:
【中文标题】如何在 ASP .Net 核心的 Swagger 中添加基本授权标头【英文标题】:How to add Basic authorization header in the Swagger in ASP .Net core 【发布时间】:2017-04-24 13:46:33 【问题描述】:如何在 Asp .Net 核心的 Swagger 中添加基本授权标头。默认情况下,api 键被视为查询字符串,但我需要对其进行自定义,以便它包含在标题中。
【问题讨论】:
【参考方案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>()
);
【讨论】:
【参考方案2】:经过几个小时的修补,我找到了这个解决方案
首先实现 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 存在 会需要的
最后将它连接到你的花哨配置
services.ConfigureSwaggerGen(options =>
options.OperationFilter<AddRequiredHeaderParameter>();
options.SingleApiVersion(new Info
Version = "v1",
Title = "Test",
Description = "Test Service",
TermsOfService = "None"
);
options.DescribeAllEnumsAsStrings();
);
希望对你有所帮助;)
【讨论】:
【参考方案3】:根据 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();
);
【讨论】:
【参考方案4】: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 作为 Authorization 标头添加到每个请求。
【讨论】:
以上是关于如何在 ASP .Net 核心的 Swagger 中添加基本授权标头的主要内容,如果未能解决你的问题,请参考以下文章
如何在ASP.Net Core的生产环境中保护swagger ui
如何在ASP.Net Core的生产环境中保护swagger ui
如何在ASP.Net Core的生产环境中保护swagger ui
[第二篇]如何在ASP.Net Core的生产环境中使用OAuth保护swagger ui