Swagger永久授权令牌

时间:2018-12-10 15:15:28

标签: asp.net-core-mvc swagger

我正在ASP.NET Core MVC中开发Web api。我想知道是否有一种方法可以使招摇中的授权令牌持久化,以便不需要在每次运行应用程序时手动进行授权。 它将使测试更加容易。

1 个答案:

答案 0 :(得分:0)

本地存储可用于保留授权令牌。

要将令牌存储到本地存储中,请在浏览器控制台中输入:

localStorage.setItem('authKey', 'the authorization token')

然后,使用请求拦截器从本地存储中提供令牌作为Authorization标头:

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;
相关问题