In Swashbuckle.AspNetCore
,授权设置全部由AddSecurityDefinition
method.
在 4.x 中,您可以设置ApiKeyScheme
描述了如何使用 API 密钥来授权请求:
c.AddSecurityDefinition("ApiKey", new ApiKeyScheme()
{
Description = "My description",
Name = "MyHttpHeaderName",
In = "header",
});
从 5.x 开始,Swashbuckle.AspNetCore
不再使用自己的模型,而是依赖于OpenAPI.NET https://github.com/microsoft/OpenAPI.NET。这意味着上述安全定义在 5.x 中将如下所示:
c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme()
{
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Name = "MyHttpHeaderName",
Description = "My description",
});
请注意,您还必须设置安全要求配置哪些操作需要哪些安全定义。在 5.x 中,其语法如下所示:
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "ApiKey" }
},
new string[] { }
}
});
您可以在以下位置阅读有关这一切的更多信息有关安全定义和要求的文档 https://github.com/domaindrivendev/Swashbuckle.AspNetCore/blob/v5.0.0-rc2/README-v5.md#add-security-definitions-and-requirements.