Out of the box there’s no way to add an Authorization header to your API requests from swagger-ui. Fortunately (if you’re using ASP.NET), Swashbuckle 5.0 is extendable, so it’s very easy to add a new IOperationFilter to do it for us:
public class AddAuthorizationHeaderParameterOperationFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) { if (operation.parameters != null) { operation.parameters.Add(new Parameter { name = "Authorization", @in = "header", description = "access token", required = false, type = "string" }); } } }
Now all you need to do is register it in your EnableSwagger call:
configuration .EnableSwagger(c => { c.SingleApiVersion("v1", "Commerce Services - Discounts"); foreach (var commentFile in xmlCommentFiles) { c.IncludeXmlComments(commentFile); } c.OperationFilter<ExamplesOperationFilter>(); c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>(); }) .EnableSwaggerUi(config => config.DocExpansion(DocExpansion.List));
Once that’s done it’ll give you an input field where you can paste your Authorization header. Don’t forget to add the word “bearer” if you’re using a JWT token:
Edit: I wrote this more than a year ago using Swashbuckle 5.2.1, it may not work with later versions.
