如何在 Swagger UI 中通过请求发送 Authorization 标头? [英] How to send Authorization header with a request in Swagger UI?

查看:54
本文介绍了如何在 Swagger UI 中通过请求发送 Authorization 标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ASP.NET Web Api 2 应用程序.我向其中添加了 Swashbuckle(用于 .NET 的 Swagger).它显示我的端点没问题,但为了发送请求,我需要将授权标头附加到该请求.如果我理解正确,我需要修改 index.html 文件 (https://github.com/swagger-api/swagger-ui#how-to-use-it) 所以我 git 克隆了 Swashbuckle 项目以修改 index.html 并添加一些标题.

I have a ASP.NET Web Api 2 application. I added Swashbuckle to it (Swagger for .NET). It displays my endpoints no problem, but in order to send a request I need to attach an Authorization header to that request. If I understand correctly in order to do that I need to modify the index.html file (https://github.com/swagger-api/swagger-ui#how-to-use-it) so I git cloned Swashbuckle project in order to modify index.html and add some headers.

这是在 Swashbuckle 中随请求发送 Authorization 标头的唯一方法吗?

Is that the only way to send Authorization header with the request in Swashbuckle?

推荐答案

为了使用 Swagger UI 发送带有请求的 Authorization 标头,我需要:

In order to send Authorization header with a request using Swagger UI I needed to:

  1. 鉴于我的程序集的名称是:My.Assembly 并且它包含一个文件夹:Swagger,在我放置自定义 index.html 的地方,我在 SwaggerConfig.cs 中添加了这一行:

  1. Given the name of my assembly is: My.Assembly and it contains a folder: Swagger, where I placed my custom index.html, I added this line in SwaggerConfig.cs:

 c.CustomAsset("index", thisAssembly, "My.Assembly.Swagger.index.html");

请注意 index.html 加载 javascript 和 css 文件.我不得不将文件路径中的所有点更改为虚线,以便加载这些文件.不知道为什么一定要这样做,但是解决了加载文件的问题...

Note that index.html loads javascript and css files. I had to change all dots to dashed in the file paths in order for those files to load. I don't know why it had to be done, but it solved the problem of loading the file...

  1. 在 index.html 文件中我修改了

  1. In the index.html file I modified the

addApiKeyAuthorization()

addApiKeyAuthorization()

函数看起来像这样:

function addApiKeyAuthorization() {
        var key = encodeURIComponent($('#input_apiKey')[0].value);
        if (key && key.trim() != "") {
            var value = "auth-scheme api_key=123456,order_id=56789";
            var authKeyHeader = new SwaggerClient.ApiKeyAuthorization("Authorization", value, "header");
            window.swaggerUi.api.clientAuthorizations.add("Authorization", authKeyHeader);
        }
    }

请注意,我将查询"更改为标题".

Note I changed "query" to "header".

  1. 我也取消了这段代码的注释:

  1. I also uncommented this code:

var apiKey = "this field represents header but can be anything as long as its not empty";
$('#input_apiKey').val(apiKey);

这将在第二个文本字段中显示文本,但只要它不为空,它包含的内容似乎并不重要.

which will display text in the second textfield, but it seems it doesn't matter what it contains as long as it is not empty.

这对我有用并使我能够加载自定义 index.html 文件.现在我正在考虑启用 Swagger UI 用户来操作标头参数的值...

That worked for me and enabled me to load custom index.html file. Now I am looking at enabling Swagger UI user to manipulate the value of header parameters...

这篇关于如何在 Swagger UI 中通过请求发送 Authorization 标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆