【问题标题】:API key in header with swashbuckle带有花饰的标头中的 API 密钥
【发布时间】: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


    【解决方案1】:

    2021 年 9 月 15 日更新:

    正如 Justin Greywolf 的评论中已经指出的那样。

    “In”和“Type”属性已从字符串更改为 ParameterLocationSecuritySchemeType 枚举:

    services.AddSwaggerGen(c =>{
        c.SwaggerDoc("v1", new Info { Title = "[anything]", Version = "v1" });
        c.AddSecurityDefinition("[auth scheme: same name as defined for asp.net]", new ApiKeyScheme() {
            In = ParameterLocation.Header,
            Name = "X-API-KEY", //header with api key
            Type = SecuritySchemeType.ApiKey,
        });
    });
    

    2019 年 4 月 10 日更新:

    范式已经转变以适应生成的 swagger.json 中的安全定义

    来源https://github.com/domaindrivendev/Swashbuckle.AspNetCore#add-security-definitions-and-requirements

    services.AddSwaggerGen(c =>{
        c.SwaggerDoc("v1", new Info { Title = "[anything]", Version = "v1" });
        c.AddSecurityDefinition("[auth scheme: same name as defined for asp.net]", new ApiKeyScheme() {
            In = "header", // where to find apiKey, probably in a header
            Name = "X-API-KEY", //header with api key
            Type = "apiKey", // this value is always "apiKey"
        });
    

    });

    原答案

    检查一下:

    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");
        })
    

    【讨论】:

    • 这对我有用。调整 swagger 配置设置时,请务必运行 iisreset 命令(如果您使用的是完整的 IIS)。配置有时会被缓存,您不会看到更改。
    • @keith 看起来这在 Swashbuckle 5.x 中再次改变了。 c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme { Description = "ApiKey 必须出现在标头中", Type = SecuritySchemeType.ApiKey, Name = "X-ApiKey", In = ParameterLocation.Header });
    【解决方案2】:

    您必须基于original(如here 所述)注入自定义index.html,并在函数addApiKeyAuthorization 中更改以下行:

    var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("X-ApiKey", key, "header");
    

    【讨论】:

    • 这作为一个解决方案很糟糕,但它似乎是唯一的方法:(
    猜你喜欢
    • 2021-08-01
    • 2020-08-20
    • 2015-07-17
    • 2023-02-22
    • 2021-04-17
    • 2018-11-25
    • 1970-01-01
    • 2011-07-27
    相关资源
    最近更新 更多