0

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.

Jay
  • 299
  • 1
  • 5
  • 20

1 Answers1

1

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

enter image description here

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

enter image description here

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
 });
Paresh
  • 993
  • 1
  • 7
  • 15
  • Hi Paresh.. Thanks for your answer.... Can you please elaborate bit more how to add custom oAuth section to Swagger – Jay Aug 18 '16 at 06:05
  • Thanks Parish... I'll give it a go – Jay Aug 19 '16 at 01:31
  • Hi Paresh... sorry not following something, cannot get floowing command executed $.get("/Swagger/swagger-oauth-section.html", function (data) { $('#resource_OAuth').html(data); }); – Jay Aug 19 '16 at 07:56
  • @Jay this is simple AJAX GET request. You need to create custom UI html file and place it under "/Swagger/swagger-oauth-section.html" to make this work. You can use any folder structure and file name. This is just an example. – Paresh Aug 19 '16 at 15:44