简体   繁体   中英

How to pass JWT bearer token for swagger UI in WebAPI

I have a WebAPI project and I use swagger UI to test them. Now I secured my Apis through OAuth2 (Auth0) and I am seeking help to pass bearer token (or user username / password, ideally) to Api calls. Please note that my client is in Angular JS and I can access secured Apis through clients. Just need to find a way to Test Apis through Swagger.

Many thanks.

You can add custom oAuth section to your Swagger UI like following

在此处输入图片说明

and then add Authorization header parameter to all your secured APIs like this

在此处输入图片说明

On click of "Get Token" update authorization parameter for all APIs if token API call is successful.

Update:

To add custom oAuth section to Swagger UI, Inject javascript file using following swagger UI configuration

            swconfig.EnableSwaggerUi(c =>
            {
                c.DocExpansion(DocExpansion.List);
                c.InjectJavaScript(thisAssembly, "Swagger.custom.js");
            });

In custom.js file on document.ready add custom html to ui plus other code to handle token API calls.

$(document).ready(function () {
    if ($('#resource_OAuth').length == 0) {
        var html = '<li id="resource_OAuth" class="resource">';
        $('#resources').prepend(html);

        $.get("/Swagger/swagger-oauth-section.html", function (data) {
            $('#resource_OAuth').html(data);
         });
     }
     //code to handle token API calls
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM