Add custom header in Swagger request for ASP.NET Core 6 using FastEndpoints

121 Views Asked by At
builder.Services.AddSwaggerDoc(settings =>
{
    settings.Title = "IPR Backend API";
    settings.Version = "v1";
    settings.AddAuth("ApiKeyAuth", new()
    {
        Name = "ApiKey",
        In = NSwag.OpenApiSecurityApiKeyLocation.Header,
        Type = NSwag.OpenApiSecuritySchemeType.ApiKey,
    });
}, addJWTBearerAuth: false);

app.UseOpenApi();
app.UseSwaggerUi3(s => s.ConfigureDefaults());

The request contains two headers ApiKey and CorrelationId which contain some values to be validated before the controller reaches the actual route through the pre processors of fastendpoints. I need to add these custom headers in my request through Swagger. Currently I am unable to run the request through Swagger and need Postman to test my APIs:

Swagger Image

1

There are 1 best solutions below

0
On

you seem to be using outdated swagger configuration methods. see this gist for a full example of api-key authentication with swagger.

and as for adding custom header parameters, you need to register a custom operation processor for nswag as shown in this example.

all of these examples are linked from the fastendpoints cookbook.