Reference

ApiKeyAuthenticationOptions

Package Trax.Api.Auth.ApiKeyNamespace Trax.Api.Auth.ApiKeySource ApiKeyAuthenticationOptions.cs

NO WARRANTY. Trax auth is plumbing, not a security product. You are solely responsible for securing systems that use it. See API Security.

The options of the API-key scheme, set through the configureOptions argument of AddTraxApiKeyAuth. It derives from AuthenticationSchemeOptions and has all of its members.

Signature

public sealed class ApiKeyAuthenticationOptions : AuthenticationSchemeOptions
{
    public string HeaderName { get; set; }
}

Properties

PropertyTypeDefaultDescription
HeaderNamestring"X-Api-Key" (ApiKeyDefaults.HeaderName)The request header that carries the key.

Example

services.AddTraxApiKeyAuth<DirectoryApiKeyResolver>(opts => opts.HeaderName = "X-Service-Key");
 
public sealed class DirectoryApiKeyResolver(IUserDirectory users) : ITraxPrincipalResolver<string>
{
    public async ValueTask<TraxPrincipal?> ResolveAsync(string apiKey, CancellationToken ct)
    {
        var user = await users.FindByApiKeyHashAsync(SHA256.HashData(Encoding.UTF8.GetBytes(apiKey)), ct);
        return user is null ? null : new TraxPrincipal(user.Id, user.DisplayName, user.Roles);
    }
}

See also