c# – 如何在ASP .Net核心的Swagger中添加基本授权头
作者:互联网
如何在Asp .Net核心中的Swagger中添加基本授权头.默认情况下,api键将其视为查询字符串,但我需要对其进行自定义以使其包含在标头中.
解决方法:
经过几个小时的修补后,我找到了这个解决方案
首先实现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; }
}
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
最后将它连接到你的swashbuckle配置
services.ConfigureSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>();
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Test",
Description = "Test Service",
TermsOfService = "None"
});
options.DescribeAllEnumsAsStrings();
});
希望能帮助到你
标签:c,asp-net-web-api,net-core,swagger,swagger-ui 来源: https://codeday.me/bug/20190611/1218402.html