在 Swashbuckle 中启用不记名令牌(Swagger 文档)

编程入门 行业动态 更新时间:2024-10-24 08:28:16
本文介绍了在 Swashbuckle 中启用不记名令牌(Swagger 文档)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个使用个人帐户安全的 asp webapi 应用程序,以便默认情况下启用不记名令牌.它运行良好,因此我可以毫无问题地在 Postman 中测试它们.

I created an asp webapi application which is using Individual Account Security so that the Bearer token is enabled by default. It's working fine so that I am able to test them in Postman without problem.

当我尝试通过 Swashbuckle 集成 Swagger UI 时出现了问题.我通过以下方式安装了 Swashbuckle:

Here comes the question when I'm trying to integrate the Swagger UI by Swashbuckle. I installed the Swashbuckle by:

安装包 Swashbuckle

Install-Package Swashbuckle

然后更改SwaggerConfig.cs:

GlobalConfiguration.Configuration .EnableSwagger(c => { c.ApiKey("Token") .Description("Filling bearer token here") .Name("Authorization") .In("header"); } .EnableSwaggerUi(c => { c.EnableApiKeySupport("Authorization", "header"); };

启动我的应用程序并填写不记名令牌:

Start my application and fill in the Bearer token:

但是当我运行需要授权的api请求时它不起作用.截图如下:

But it doesn't work when I run the api request which need authorization. Here is the screenshot:

不记名令牌添加到标题中的授权.但我仍然收到错误 401.我想知道是不是因为令牌被编码(空格被 %20 替换)?任何的想法?谢谢.

The bearer token is added to Authorization in header. But I still got error 401. I'm wondering if it's because the token is encoded (the SPACE is replaced by %20)? Any idea? Thanks.

顺便说一下,我想知道如何在我的 Swagger 文档中添加 /token 以便我可以在 Swagger UI 中获取令牌.

By the way, I'm wondering how to add the /token in my Swagger document too so that I can get the token in Swagger UI.

推荐答案

更新

下面详述的问题现已在 Swashbuckle v5.5.0 中得到解决.

刚刚遇到了完全相同的问题.我认为根本原因是 中的这一行Swashbuckle 的源代码:

Just ran into the exact same issue. I think the root cause is this line in Swashbuckle's source code:

var key = encodeURIComponent($('#input_apiKey')[0].value);

这是来自 HTML 输入字段的值通过 URL 编码将空格转换为 %20 的地方.我打算在 GitHub 上的 Swashbuckle 存储库 中打开一个问题.

This is where the value from the HTML input field goes through URL encoding turning the space into %20. I'm planning to open an issue in the Swashbuckle repo on GitHub.

在该问题得到解决之前,这里是一种基于使用注入 Swagger UI 的 Javascript 文件替换上述行的解决方法:

Until that issue is resolved, here is a workaround based on replacing the above line using a Javascript file injected into the Swagger UI:

  • 在安装了 Swashbuckle 的项目中,创建一个新文件夹并将其命名为Swagger".

    在新文件夹中创建一个名为 "SwaggerUiCustomization.js" 的新 Javascript 文件并将此脚本放入其中:

    In the new folder create a new Javascript file called "SwaggerUiCustomization.js" and put this script in it:

    (function () { function addApiKeyAuthorization() { var key = $('#input_apiKey')[0].value; if (key && key.trim() != "") { var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization(swashbuckleConfig.apiKeyName, key, swashbuckleConfig.apiKeyIn); window.swaggerUi.api.clientAuthorizations.add("api_key", apiKeyAuth); log("added key " + key); } } $('#input_apiKey').change(addApiKeyAuthorization); })();

  • 在解决方案资源管理器中,选择文件并按 Alt+Enter 编辑其属性.在 Properties 窗口中,将文件的 Build Action 更改为 Embedded Resource.

  • In the Solution Explorer, choose the file and hit Alt+Enter to edit its Properties. In the Properties window change the file's Build Action to Embedded Resource.

    在您的 SwaggerConfig.cs 文件中,在 EnableSwaggerUi() 代码块中添加以下行:c.InjectJavaScript(thisAssembly,"<Project_Default_Namespace>.Swagger.SwaggerUiCustomization.js");当然,请务必将 替换为您项目的默认命名空间.

    In your SwaggerConfig.cs file add the following line inside the EnableSwaggerUi() code block: c.InjectJavaScript(thisAssembly, "<Project_Default_Namespace>.Swagger.SwaggerUiCustomization.js"); Be sure, of course, to replace <Project_Default_Namespace> with your project's default namespace.

    运行您的项目并在文本框中输入Bearer".当您调用控制器操作时,您应该在服务器端获得完全相同的值 - 使用空格而不是 %20%.

    Run your project and enter "Bearer " into the text box. When you invoke a controller action, you should get this exact same value - with a whitespace instead of %20% - on the server side.

  • 更多推荐

    在 Swashbuckle 中启用不记名令牌(Swagger 文档)

    本文发布于:2023-11-03 14:56:49,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1555408.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:令牌   文档   Swashbuckle   Swagger

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!