Last active
May 13, 2018 08:13
-
-
Save iwannabebot/5e9f4087f05b37d285b1a4b557e88e17 to your computer and use it in GitHub Desktop.
Add Bearer token in header of a Swagger request
This file contains 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
public void ConfigureServices(IServiceCollection services) | |
{ | |
services | |
.AddSwaggerGen(c => | |
{ | |
c.OperationFilter<AddAuthorizationHeaderParameterOperationFilter>(); | |
}); | |
} | |
/// <summary> | |
/// Add auth header in your request middleware | |
/// </summary> | |
public class AddAuthorizationHeaderParameterOperationFilter : IOperationFilter | |
{ | |
public void Apply(Operation operation, OperationFilterContext context) | |
{ | |
if(operation.Parameters == null) | |
{ | |
operation.Parameters = new List<IParameter>(); | |
} | |
operation.Parameters.Add(new NonBodyParameter | |
{ | |
Name = "Authorization", | |
In = "header", | |
Type = "string", | |
Description = "access token", | |
Required = false | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment