【发布时间】:2018-09-26 04:02:27
【问题描述】:
我有一个 web api2 项目,我在其中实现了 swashbuckle 以测试和记录我的 web 服务。 我试图在 SwaggerDocsConfig 中为身份验证设置一个 apiKey 并且它可以工作,但是如果我想添加另一个 apiKey(apiKey 和 appId)我不能它有效。
我在swagger doc 中读到这是可能的,但我不知道如何使用 swashbuckle 以这种方式配置 swagger 文档。
Swagger 文档应该是怎样的
securityDefinitions:
apiKey:
type: apiKey
in: header
name: X-API-KEY
appId:
type: apiKey
in: header
name: X-APP-ID
security:
- apiKey: []
appId: []
当我在我的项目中启用 swagger 时,我尝试简单地添加另一个 ApiKey(参见上面的代码),但它不起作用。
GlobalConfiguration.Configuration.EnableSwagger(swagger =>
{
swagger.RootUrl(req => req.RequestUri.GetLeftPart(UriPartial.Authority).TrimEnd('/') + req.GetRequestContext().VirtualPathRoot.TrimStart('/'));
swagger.PrettyPrint();
c.SingleApiVersion("v1", "Project.WebApi");
swagger.ApiKey("apiKey") //First ApiKey
.Description("API Key Authentication")
.Name("Authorization")
.In("header");
swagger.ApiKey("apiId") //Second ApiKey
.Description("API Key Authentication")
.Name("Authorization") //Same Schema
.In("header");
swagger.IncludeXmlComments(string.Format(@"{0}\bin\Project.WebApi.XML", System.AppDomain.CurrentDomain.BaseDirectory));
swagger.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
})
.EnableSwaggerUi(swagger =>
{
swagger.DocumentTitle("Project API");
swagger.DocExpansion(DocExpansion.List);
swagger.EnableDiscoveryUrlSelector();
swagger.EnableApiKeySupport("Authorization", "header");
});
是否可以使用 Swashbuckle 来完成,或者我必须注入一个 js 脚本并从客户端执行?
谢谢
【问题讨论】:
-
您使用的是什么版本的 Swashbuckle?
-
当前可用的最新版本:5.6.0
标签: c# asp.net-web-api2 swagger api-key swashbuckle