Class ApiKeyService
Validates API keys against .
Keys are hashed with HMAC-SHA256 before lookup.
Validated results are cached for to avoid repeated store calls.
Comparison uses constant-time equality via .
public sealed class ApiKeyService
Inheritance
Properties
CacheTtl
Full cache lifetime for a validated key. After this the key is fully re-validated
public TimeSpan CacheTtl { get; set; }
RevocationCheckInterval
How often a still-cached key is cheaply re-checked against the store for revocation/expiry
public TimeSpan RevocationCheckInterval { get; set; }
Methods
CreateKeyAsync(string, string?, string?, DateTimeOffset?, CancellationToken)
Create a new API key, store its hash, and return the raw key (shown only once).
public Task<(string RawKey, ApiKeyRecord Record)> CreateKeyAsync(string name, string? userId = null, string? roles = null, DateTimeOffset? expiresAt = null, CancellationToken ct = default)
HashKey(string)
Hash a raw API key to its HMAC-SHA256 hex representation.
public string HashKey(string rawKey)
ListKeysAsync(CancellationToken)
List all keys (admin).
public Task<IReadOnlyList<ApiKeyRecord>> ListKeysAsync(CancellationToken ct = default)
RevokeKeyAsync(string, CancellationToken)
Revoke a key and remove it from cache.
public Task<bool> RevokeKeyAsync(string id, CancellationToken ct = default)
ValidateAsync(string, CancellationToken)
Validate a raw API key. Returns the matching if valid, null otherwise.
public Task<ApiKeyRecord?> ValidateAsync(string rawKey, CancellationToken ct = default)