加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-web-api – 标题中的API密钥与swashbuckle

发布时间:2020-12-15 19:24:05 所属栏目:asp.Net 来源:网络整理
导读:我想在一个带有Swashbuckle的WebAPI项目上进行基于API密钥的身份验证(swagger for .net). 我已将swashbuckle配置如下: config .EnableSwagger(c = { c.ApiKey("apiKey") .Description("API Key Authentication") .Name("X-ApiKey") .In("header"); c.Single
我想在一个带有Swashbuckle的WebAPI项目上进行基于API密钥的身份验证(swagger for .net).

我已将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文件:

   "securityDefinitions": {
      "apiKey": {
        "type": "apiKey","description": "API Key Authentication","name": "X-ApiKey","in": "header"
      }
    }

但是当我转到UI并“试一试”时,它会尝试将API密钥放入查询字符串(我认为是默认行为)而不是标题.

例如:

curl -X POST –header’Eccept:application / json”http:// localhost:63563 / api / MyMethod?api_key = key’

如何使用swigger将API密钥放在标头而不是查询字符串中?

解决方法

看看这个:
config
    .EnableSwagger(c =>
    {
        c.ApiKey("apiKey")
            .Description("API Key Authentication")
            .Name("X-ApiKey")
            .In("header");
        c.SingleApiVersion("v1","My API");

    })
    .EnableSwaggerUi(c => {
        c.EnableApiKeySupport("X-ApiKey","header");
    })

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读