API key authentication from Regira.Security.Authentication — for machine callers that cannot perform an interactive sign-in.
| Property | Type | Default | Description |
|---|---|---|---|
ApiKeyHeaderName |
string |
"X-Api-Key" |
Request header name |
AuthenticationType |
string |
"ApiKey" |
Authentication type string |
Task<ApiKeyOwner?> FindByOwner(string id);
Task<ApiKeyOwner?> FindByKey(string apiKey);
Task<bool> Validate(string id, string apiKey);
| Property | Type | Description |
|---|---|---|
OwnerId |
string |
Owner identifier |
Key |
string |
API key value |
Roles |
ICollection<string> |
Roles assigned to this key |
Claims |
ICollection<ApiKeyOwner.Claim> |
Extra claims (Type / Value pairs) added to the principal |
var services = new ServiceCollection();
IConfiguration configuration = new ConfigurationManager();
// In-memory keys from code
services.AddApiKeyAuthentication()
.AddInMemoryApiKeyAuthentication(new[]
{
new ApiKeyOwner { OwnerId = "client-a", Key = "key-abc", Roles = ["read"] }
});
// From appsettings.json
var keys = configuration.GetSection(AuthenticationSections.ApiKeys).ToApiKeyOwners();
services.AddApiKeyAuthentication()
.AddInMemoryApiKeyAuthentication(keys);
appsettings.json shape — an array, each entry carrying its own OwnerId. ToApiKeyOwners() requires both
fields, so an object keyed by owner name throws InvalidOperationException at startup:
"ApiKeys": [
{ "OwnerId": "client-a", "Key": "key-abc", "Roles": ["read", "write"] }
]
The keys themselves belong in user-secrets or the environment (Authentication:ApiKeys:0:Key), not in
appsettings.json; configuration merges the entries by index. A key never set throws at startup, naming the path to
set.
AddApiKeyAuthentication() registers no IApiKeyOwnerService; the handler resolves whichever is registered, and
AddInMemoryApiKeyAuthentication is one. For keys in your database, implement it over your DbContext and register
it instead:
using Regira.Security.Authentication.ApiKey.Abstraction; // IApiKeyOwnerService
services.AddScoped<IApiKeyOwnerService, DbApiKeyOwnerService>();
services.AddApiKeyAuthentication();
FindByKey, and null gives 401.RevokedAt that FindByKey filters on.AddInMemoryApiKeyAuthentication, since the last registration wins.Apache License 2.0 — this package contains no license validation and no runtime limits. See LICENSE. A few companion packages are commercially licensed with a free tier; see the licensing overview.