asp.net-core-mvc - Swagger permanent authorization token
问题描述
I am developing web api in ASP.NET Core MVC. I wonder if there is a way to make authorization token in swagger persistent, so that the authorization does not need to be done manually every time the application is run. It would make testing easier.
解决方案
The local storage can be used for keeping the authorization token.
To store the token into the local storage, type into the browser console:
localStorage.setItem('authKey', 'the authorization token')
Then, use request interceptor to supply the token from the local storage as Authorization header:
const ui = SwaggerUIBundle({
url: "/swagger/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
requestInterceptor: function (req) {
var key = localStorage.getItem("authKey");
if (key && key.trim() !== "") {
req.headers.Authorization = 'Bearer ' + key;
console.log('Authorized from authKey');
}
},
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout",
})
window.ui = ui;
推荐阅读
- angularjs - 控制器中的AngularJS日期未定义
- vue.js - vue.js 如何将值建模为单独的数组
- python - 梯度下降实施python - 等高线
- jenkins - 詹金斯跳过自由式工作链中的一些工作
- docker - Kafka Streams 消费者组并发:仅从一个分区消费?
- wordpress - Woocommerce - 产品单页中的作者简介
- javascript - 使用带有 office-js-helpers 身份验证器的 msgraph-sdk-javascript 的令牌错误
- swagger - Swagger 如何显示模型中的所有字段但仅显示示例值中的某些字段(SpringBoot 项目)
- swift - 使用协议集获取具有 nil 值的 var
- python - 如何在python中遍历字典中的键和值