Last active
May 5, 2019 09:39
-
-
Save rrharvey/da1fe67cb43fe3059de2d8aa6f67a825 to your computer and use it in GitHub Desktop.
Add bearer token to Swagger UI using Swashbuckle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
$(function () { | |
$('#input_apiKey').attr('placeholder', 'JSON Web Token'); | |
$('#input_apiKey').off(); | |
$('#input_apiKey').on('change', function () { | |
var key = this.value; | |
console.info('Set bearer token to: ' + key); | |
if (key && key.trim() !== '') { | |
swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + key, "header")); | |
} | |
}); | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Swashbuckle.Application; | |
using System; | |
using System.Reflection; | |
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")] | |
namespace MyApplication | |
{ | |
public class SwaggerConfig | |
{ | |
public static void Register() | |
{ | |
var thisAssembly = typeof(SwaggerConfig).Assembly; | |
GlobalConfiguration.Configuration | |
.EnableSwagger(c => | |
{ | |
// Use "SingleApiVersion" to describe a single version API. Swagger 2.0 includes an "Info" object to | |
// hold additional metadata for an API. Version and title are required but you can also provide | |
// additional fields by chaining methods off SingleApiVersion. | |
// | |
c.SingleApiVersion("v1", "MyApplication"); | |
}) | |
.EnableSwaggerUi(c => | |
{ | |
// Use the "InjectJavaScript" option to invoke one or more custom JavaScripts after the swagger-ui | |
// has loaded. The file must be included in your project as an "Embedded Resource", and then the resource's | |
// "Logical Name" is passed to the method as shown above. | |
c.InjectJavaScript(thisAssembly, "MyApplication.CustomSwagger.js"); | |
}); | |
} | |
} | |
} |
@janmohammadi - that is a great solution and worked excellent! THANK YOU!
@janmohammadi,
can you give me more details? Where did you enter this script?
do you have an example with all distribution files?
does this feature supports in v5.6?
Works like a charm! Thanks a lot.
This script is only Swashbuckle for .net framework that uses old version of Swagger-ui (v2.2.10). Swashbuckle ASP.NET Core (v3) uses swagger-ui v3.x.
- To add Jquery support, I customized swagger index.html.
- There is no '#input_apiKey' element. Used new selectors at CustomSwagger.js
- There is no 'swaggerUi.api.clientAuthorizations' so used react.js functions to set api key input.
I changed @janmohammadi 's script to support swagger-ui v3. Here is the link
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I improved this script as flow, that can accept , in that text box.
`(function () {
})();`