How to send Authorization header with a request in Swagger UI?

后端 未结 4 1031
孤独总比滥情好
孤独总比滥情好 2020-12-31 02:33

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

4条回答
  •  太阳男子
    2020-12-31 03:15

    I added below code in a js file and added it as a embedded resource to my web api project. When you build and run Swagger, api_key textbox will get replaced with Authorization Key Text Box, where you can paste your AuthKey and with every request, swagger will add it to Request header.

    (function () {
    
        $(function () {
            var basicAuthUI =
             '
    '; $(basicAuthUI).insertBefore('#api_selector div.input:last-child'); $("#input_apiKey").hide(); $('#input_token').change(addAuthorization); }); function addAuthorization() { var token = $('#input_token').val(); if (token && token.trim() !== "" ) { window.swaggerUi.api.clientAuthorizations.add("api_key", new window.SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + token, "header")); console.log("authorization added: Bearer = " + token); } } })();

提交回复
热议问题