Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
dj-nitehawk / ApiKeyAuth.cs
Last active April 5, 2025 13:49
API Key Authentication With FastEndpoints + Swagger
sealed class ApikeyAuth(IOptionsMonitor<AuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
IConfiguration config)
: AuthenticationHandler<AuthenticationSchemeOptions>(options, logger, encoder)
{
internal const string SchemeName = "ApiKey";
internal const string HeaderName = "x-api-key";
readonly string _apiKey = config["Auth:ApiKey"] ?? throw new InvalidOperationException("Api key not set in appsettings.json");
@dj-nitehawk
dj-nitehawk / AddCustomHeader.cs
Created September 18, 2023 02:07
Customizing Swagger Spec With An IOperationProcessor
internal sealed class AddCustomHeader : IOperationProcessor
{
public bool Process(OperationProcessorContext context)
{
var hdrParameter = new OpenApiParameter()
{
Name = "x-custom",
Kind = OpenApiParameterKind.Header,
IsRequired = true,
Type = JsonObjectType.String,