Reputation: 91
How to add basic authorization header in Swagger in Asp .Net core. By default the api key treats as a query string but I need to customize that so that it will include in the header.
Upvotes: 9
Views: 16144
Reputation: 1503
If you are using Swashbuckle.AspNetCore v5.0.0-rc2 see below:
c.AddSecurityDefinition("Basic", new OpenApiSecurityScheme
{
Description = "Basic auth added to authorization header",
Name = "Authorization",
In = ParameterLocation.Header,
Scheme = "basic",
Type = SecuritySchemeType.Http
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Basic" }
},
new List<string>()
}
});
Upvotes: 19
Reputation: 197
To place in swagger-ui navbar you can use this code:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
...
});
c.AddSecurityDefinition("JWT Token", new ApiKeyScheme
{
Description = "JWT Token",
Name = "Authorization",
In = "header"
});
});
It will add JWT Token as Authorization header to every request.
Upvotes: 2
Reputation: 93
Based on Mohsen's reply figured out we can do this without having to implement the Iparameter interface
public class AddRequiredHeaderParameter : IOperationFilter
{
void IOperationFilter.Apply(Operation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(new NonBodyParameter
{
Name = "Authorization",
In = "header",
Description = "JWT Token",
Required = true,
Type = "string"
});
}
}
and finally hook it up to your swashbuckler config
services.ConfigureSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>();
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Test",
Description = "Test Service",
TermsOfService = "None"
});
options.DescribeAllEnumsAsStrings();
});
Upvotes: 2
Reputation: 1872
after hours of tinkering I found this solution
first implement the IOperationFilter as below:
public class AddRequiredHeaderParameter : IOperationFilter
{
void IOperationFilter.Apply(Operation operation, OperationFilterContext context)
{
var param = new Param();
param.Name = "authorization";
param.In = "header";
param.Description = "JWT Token";
param.Required = true;
param.Type = "string";
if (operation.Parameters == null)
operation.Parameters = new List<IParameter>();
operation.Parameters.Add(param);
}
}
then implement the interface IParameter
class Param : IParameter
{
public string Description { get; set; }
public Dictionary<string, object> Extensions { get {return new Dictionary<string, object>{{"test", true}};} }
public string In { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public bool Required { get; set; }
}
the VERY important thing here is the Type property which is not required by the interface but it has to be there as the swagger-ui will need it
and finally hook it up to your swashbuckle config
services.ConfigureSwaggerGen(options =>
{
options.OperationFilter<AddRequiredHeaderParameter>();
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "Test",
Description = "Test Service",
TermsOfService = "None"
});
options.DescribeAllEnumsAsStrings();
});
Hope it helps ;)
Upvotes: 4