I have added a header parameter to Swagger for the API Key and this is working as far as providing the ability to enter the API key. I am using the following
- Swashbuckle.AspNetCore 6.1
- Swashbuckle.AspNetCore.Swagger 6.1
- Swashbuckle.AspNetCore.SwaggerGen 6.1
- Swashbuckle.AspNetCore.SwaggerUI 6.1
My code is as follows, in the Startup.cs I have the following for adding the location of the Swagger config
public void ConfigureServices(IServiceCollection services)
services.AddSwaggerGen(config =>
{
config.OperationFilter<SwaggerFilter>();
});
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swift Api V1");
c.RoutePrefix = string.Empty;
});
The Swaggerfilter code in it's entirety is
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Collections.Generic;
namespace SwiftApi.Api
{
public class SwaggerFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();
operation.Parameters.Add(new OpenApiParameter
{
Name = "Authorization",
In = ParameterLocation.Header,
Schema = new OpenApiSchema { Type = "string" },
Description = "API Key",
Required = true
});
}
}
}
However in method to check the value passed in the header, this is not found in my list of header values
Having looked online I cannot see anything I am doing wrong, but clearly I have omitted something, when I use Postman I can see the header value.
This is the first time I have tried doing this so It is likely I have missed something out, the question is what?
The values retrieved from Postman
Any suggestions would be gratefully appreciated.