Tharga.Api
0.0.1-pre.5
dotnet add package Tharga.Api --version 0.0.1-pre.5
NuGet\Install-Package Tharga.Api -Version 0.0.1-pre.5
<PackageReference Include="Tharga.Api" Version="0.0.1-pre.5" />
<PackageVersion Include="Tharga.Api" Version="0.0.1-pre.5" />
<PackageReference Include="Tharga.Api" />
paket add Tharga.Api --version 0.0.1-pre.5
#r "nuget: Tharga.Api, 0.0.1-pre.5"
#:package Tharga.Api@0.0.1-pre.5
#addin nuget:?package=Tharga.Api&version=0.0.1-pre.5&prerelease
#tool nuget:?package=Tharga.Api&version=0.0.1-pre.5&prerelease
Tharga Api
Reusable API-key authentication, authorization, controller registration, and OpenAPI/Swagger setup for ASP.NET Core projects. Targets .NET 9.0 and .NET 10.0.
Features
- API key authentication –
AuthenticationHandlerthat reads theX-API-KEYheader, validates it against a store, and populatesTeamKey,Name, andAccessLevelclaims. - Access level authorization –
[RequireAccessLevel]attribute withAccessLevelProxy<T>for declarative, service-level authorization enforcement viaDispatchProxy. - Controller registration –
AddThargaControllersregisters MVC controllers, OpenAPI document generation (with API key security scheme), Swagger UI, and endpoints API explorer in a single call. - API key management –
IApiKeyAdministrationServiceprovides key lookup, listing, refresh, and lock operations. A default MongoDB-backed implementation is included. - Pluggable storage – Implement
IApiKeyAdministrationServiceto use your own data store, or use the built-inApiKeyAdministrationServicewith Tharga.MongoDB.
Getting started
1. Install the package
dotnet add package Tharga.Api
2. Register controllers and authentication
// Program.cs
builder.Services.AddThargaControllers(o =>
{
o.SwaggerTitle = "My API v1";
});
builder.Services.AddAuthentication()
.AddThargaApiKeyAuthentication();
// If using the built-in MongoDB key store:
builder.Services.AddThargaApiKeys();
3. Map controllers and Swagger
var app = builder.Build();
app.UseThargaControllers();
app.UseAuthentication();
app.UseAuthorization();
app.Run();
4. Protect endpoints
[Authorize(Policy = ApiKeyConstants.PolicyName)]
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
var teamKey = User.FindFirst(TeamClaimTypes.TeamKey)?.Value;
return Ok(new { teamKey });
}
}
5. Access level enforcement on services
Decorate service interface methods with [RequireAccessLevel] and register with AddScopedWithAccessLevel:
public interface IMyService
{
[RequireAccessLevel(AccessLevel.Viewer)]
IAsyncEnumerable<Item> GetAsync();
[RequireAccessLevel(AccessLevel.User)]
Task<Item> AddAsync(string name);
[RequireAccessLevel(AccessLevel.Administrator)]
Task DeleteAsync(string key);
}
// Program.cs
builder.Services.AddScopedWithAccessLevel<IMyService, MyService>();
The proxy reads TeamKey and AccessLevel claims from HttpContext.User. Methods without the attribute throw InvalidOperationException (fail-closed).
6. API key access level
The AccessLevel for API keys is determined by:
ApiKeyEntity.AccessLevelfield (stored on the entity). If null (legacy keys), defaults toAdministratorfor backwards compatibility.IApiKey.Tags["AccessLevel"]fallback (custom implementations). If not set, defaults toViewer(fail-closed).- Auto-created keys in simple mode get
Useraccess level.
The access level hierarchy is:
| Level | Can access |
|---|---|
Owner |
Everything |
Administrator |
Everything (same as Owner) |
User |
User, Viewer |
Viewer |
Viewer only |
7. Custom key service (optional)
To use your own storage backend, implement IApiKeyAdministrationService and register it:
builder.Services.AddAuthentication()
.AddThargaApiKeyAuthentication<MyCustomKeyService>();
Public API
| Type | Description |
|---|---|
AccessLevel |
Enum: Owner, Administrator, User, Viewer. |
TeamClaimTypes |
Claim type constants: TeamKey, AccessLevel. |
RequireAccessLevelAttribute |
Declares minimum access level for a service method. |
AccessLevelProxy<T> |
DispatchProxy enforcing [RequireAccessLevel] via claims. |
AccessLevelServiceCollectionExtensions |
AddScopedWithAccessLevel<TService, TImpl>() registration helper. |
IApiKey |
Interface representing an API key with metadata (Key, Name, ApiKey, TeamKey, Tags). |
IApiKeyAdministrationService |
Service interface for key lookup, listing, refresh, and lock. |
ApiKeyConstants |
Well-known constants: HeaderName, SchemeName, PolicyName. |
ApiKeyRegistration |
Extension methods: AddThargaApiKeyAuthentication, AddThargaApiKeyAuthentication<T>. |
ControllersRegistration |
Extension methods: AddThargaControllers, AddThargaApiKeys, UseThargaControllers. |
ThargaControllerOptions |
Options for Swagger title and route prefix. |
ApiKeyEntity |
Default MongoDB entity implementing IApiKey. |
IApiKeyRepository |
Repository interface for API key persistence. |
Dependencies
- Tharga.MongoDB – MongoDB repository infrastructure and auto-registration.
- Tharga.Toolkit – Shared utilities including API key hashing.
- Swashbuckle.AspNetCore – Swagger UI generation.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.OpenApi (>= 10.0.5)
- Swashbuckle.AspNetCore (>= 10.1.5)
- Tharga.MongoDB (>= 2.8.5-pre.20)
- Tharga.Toolkit (>= 1.15.18)
-
net9.0
- Microsoft.AspNetCore.OpenApi (>= 9.0.14)
- Swashbuckle.AspNetCore (>= 10.1.5)
- Tharga.MongoDB (>= 2.8.5-pre.20)
- Tharga.Toolkit (>= 1.15.18)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1-pre.5 | 82 | 3/15/2026 |
| 0.0.1-pre.3 | 72 | 3/13/2026 |
| 0.0.1-pre.2 | 65 | 3/13/2026 |