【发布时间】:2019-09-04 23:46:38
【问题描述】:
我有基本的 Api,它接受 my-api-key 的默认标头值和相应的值。
我正在尝试让 Swagger UI 允许我一次输入标题以进行授权,并让键/值与每个请求一起传递。
到目前为止,我只是成功地将标头作为参数显式添加到每个端点,但这并不理想。
相关代码sn-ps:
services.AddApiVersioning(
options =>
{
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(
options =>
{
// add the versioned api explorer, which also adds IApiVersionDescriptionProvider service
// note: the specified format code will format the version as "'v'major[.minor][-status]"
options.GroupNameFormat = "'v'VVV";
// note: this option is only necessary when versioning by url segment. the SubstitutionFormat
// can also be used to control the format of the API version in route templates
options.SubstituteApiVersionInUrl = true;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api", Version = "v1" });
c.SwaggerDoc("v2", new OpenApiInfo { Title = "Api", Version = "v2" });
// this isn't ideal as I have to fill in the Api Key on ever request
//c.OperationFilter<ApiKeySwaggerFilter>();
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.ApiKey,
Name = "my-api-key",
In = ParameterLocation.Header
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ new OpenApiSecurityScheme()
{
// Type = SecuritySchemeType.ApiKey,
Name = ""
//In = ParameterLocation.Header
//Reference = new OpenApiReference()
//{
// Id = "myToken",
// Type = ReferenceType.SecurityScheme
//},
}, new string[] { }
}
});
});
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api v1");
c.SwaggerEndpoint("/swagger/v2/swagger.json", "Api v2");
});
我所在位置的对应图片:
对应的最小峰值:https://github.com/aherrick/SwaggerSample
我觉得这很接近,但是如何让 Api Header 在每个请求中传递,而不必强制用户在每个方法请求上填写参数。
【问题讨论】:
-
用户点击绿色的“授权”按钮并在此处输入 API 密钥。然后这个 API 密钥将与所有请求一起发送。这不是你需要的吗?
标签: swagger swagger-ui openapi swashbuckle