I have the following IOperationFilter
class, which implements the authentication headers required for some endpoints in my API application:
public class AuthenticationHeadersFilter : IOperationFilter
{
public void Apply(OpenApiOperation operation, OperationFilterContext context)
{
if (operation.Parameters == null)
operation.Parameters = new List<OpenApiParameter>();
operation.Parameters.Add(new OpenApiParameter
{
Name = "AccountName",
In = ParameterLocation.Header,
Required = true
});
operation.Parameters.Add(new OpenApiParameter
{
Name = "ApiKey",
In = ParameterLocation.Header,
Required = true
});
}
}
The above is added to my application's Swagger UI through the following in the ConfigureServices
method of Startup.cs
:
services.AddSwaggerGen(c =>
{
// ...
// other Swagger configurations
// ...
c.OperationFilter<AuthenticationHeadersFilter>();
});
This works well, however I now also have some endpoints which I'd like to be documented/displayed by Swagger, but should be completely publicly accessible without requiring the user to supply the AccountName
and ApiKey
as headers in their API request. How can I accomplish this?
I found this Stack Overflow answer, but not sure if it can be adapted for my purpose here. I wasn't able to find any useful documentation regarding the OperationFilterContext
class.
Any help is greatly appreciated.