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

我想在一个带有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");
    })

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...