Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
dj-nitehawk / ApiKeyAuth.cs
Last active March 5, 2024 15:17
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");