Skip to content

Instantly share code, notes, and snippets.

@iwannabebot
Last active May 13, 2018 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iwannabebot/5e9f4087f05b37d285b1a4b557e88e17 to your computer and use it in GitHub Desktop.
Save iwannabebot/5e9f4087f05b37d285b1a4b557e88e17 to your computer and use it in GitHub Desktop.
Add Bearer token in header of a Swagger request
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