You can do this differently depending on how you compile the Authorization header and whether you want the code to handle everything or if you want the user to enter the Authorization header that they want.
When I first tried this, I was able to show the Authorization header text in each area of ββthe endpoint parameter field, where the user could enter the Authorization header, but that was not what I wanted.
In my situation, I had to send a request to the /token endpoint using a user cookie in order to get a valid Authorization token. So I did something to achieve this.
First, in SwaggerConfig.cs I uncommented c.BasicAuth() to get the main auth scheme in the API scheme, and I also added a custom index.html page where I inserted an AJAX request to capture the Authorization token using the user's cookie ( index.html shown below):
public static void Register() { System.Reflection.Assembly thisAssembly = typeof(SwaggerConfig).Assembly; System.Web.Http.GlobalConfiguration.Configuration .EnableSwagger(c => { ... c.BasicAuth("basic").Description("Bearer Token Authentication"); ... }) .EnableSwaggerUi(c => { ... c.CustomAsset("index", thisAssembly, "YourNamespace.index.html"); ... }); }
Then run here to download the swashbuckle index.html , which we will configure to insert the Authorization header.
Below, I just make an AJAX call to my endpoint /token with a valid cookie, get the Authorization token and pass it to swagger for use with window.swaggerUi.api.clientAuthorizations.add() :
... function log() { if ('console' in window) { console.log.apply(console, arguments); } } $.ajax({ url: url + 'token' , type: 'POST' , data: { 'grant_type': 'CustomCookie' } , contentType: 'application/x-www-form-urlencoded' , async: true , timeout: 60000 , cache: false , success: function(response) { console.log('Token: ' + response['token_type'] + ' ' + response['access_token']); window.swaggerUi.api.clientAuthorizations.add("key", new SwaggerClient.ApiKeyAuthorization("Authorization", response['token_type'] + ' ' + response['access_token'], "header")); } , error: function(request, status, error) { console.log('Status: ' + status + '. Error: ' + error + '.'); } });
I removed a few things from the AJAX call to make it simpler and obviously your implementation will probably differ depending on how you collect the token, etc., but this gives you an idea. If you have any specific questions or questions, let me know.
* Edit: I didnβt notice that you really wanted the user to enter their Authorization header. In this case, it is very easy. I used this post. Just create the following class to do the job:
public class AddRequiredHeaderParameter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { if (operation.parameters == null) { operation.parameters = new List<Parameter>(); } operation.parameters.Add(new Parameter { name = "Foo-Header", @in = "header", type = "string", required = true }); } }
Then we added the class to my SwaggerConfig as follows:
... c.OperationFilter<AddRequiredHeaderParameter>(); ...