【发布时间】:2016-05-02 05:14:49
【问题描述】:
我想使用 Swashbuckle (swagger for .net) 对 WebAPI 项目进行基于 API 密钥的身份验证。
我已将 swashbuckle 配置如下:
config
.EnableSwagger(c =>
{
c.ApiKey("apiKey")
.Description("API Key Authentication")
.Name("X-ApiKey")
.In("header");
c.SingleApiVersion("v1", "My API");
})
.EnableSwaggerUi();
(见https://github.com/domaindrivendev/Swashbuckle#describing-securityauthorization-schemes)
它似乎创建了我期望的 swagger 文件:
“安全定义”:{ “apiKey”:{ “类型”:“apiKey”, "description": "API 密钥认证", "name": "X-ApiKey", “在”:“标题” } }但是当我进入 UI 并“尝试一下”时,它会尝试将 API 密钥放入查询字符串(我认为这是默认行为)而不是标题中。
例如:
curl -X POST --header 'Accept: application/json' 'http://localhost:63563/api/MyMethod?api_key=key'
我怎样才能大摇大摆地使用将 API 密钥放在标头而不是查询字符串中?
【问题讨论】:
标签: asp.net-web-api swagger swagger-ui swashbuckle