OwnershipGuard 0.2.0
See the version list below for details.
dotnet add package OwnershipGuard --version 0.2.0
NuGet\Install-Package OwnershipGuard -Version 0.2.0
<PackageReference Include="OwnershipGuard" Version="0.2.0" />
<PackageVersion Include="OwnershipGuard" Version="0.2.0" />
<PackageReference Include="OwnershipGuard" />
paket add OwnershipGuard --version 0.2.0
#r "nuget: OwnershipGuard, 0.2.0"
#:package OwnershipGuard@0.2.0
#addin nuget:?package=OwnershipGuard&version=0.2.0
#tool nuget:?package=OwnershipGuard&version=0.2.0
OwnershipGuard
OwnershipGuard is an ASP.NET Core library that enforces ownership checks and optional tenant checks before endpoint or MVC action execution. It centralizes resource access validation by route id and helps prevent IDOR and broken access control caused by missing per-endpoint checks.
Requirements
- .NET 8
- ASP.NET Core 8
Installation
dotnet add package OwnershipGuard
<PackageReference Include="OwnershipGuard" Version="x.y.z" />
Quick Start
- Register services.
using System.Security.Claims;
builder.Services.AddOwnershipGuard(options =>
{
options.UserIdClaimType = ClaimTypes.NameIdentifier;
options.TenantIdClaimType = "tenant_id";
options.UseProblemDetailsResponses = true;
options.HideExistenceWhenForbidden = false;
});
- Register descriptors after
builder.Build().
var app = builder.Build();
var registry = app.Services.GetRequiredService<IOwnershipDescriptorRegistry>();
// String key + ownership check
registry.Register<Note>(
sp => sp.GetRequiredService<YourDbContext>().Notes,
n => n.Id,
n => n.OwnerId);
// Typed key (Guid) + ownership + tenant check
registry.Register<Document, Guid>(
sp => sp.GetRequiredService<YourDbContext>().Documents,
d => d.Id,
d => d.OwnerId,
tenantSelector: d => d.TenantId);
- Apply the check to endpoints.
Minimal API:
app.MapGet("/documents/{id}", ...).RequireOwnership<Document>("id");
app.MapPut("/documents/{id}", ...).RequireOwnership<Document>("id");
MVC:
[ApiController]
[Route("documents")]
[RequireOwnership("id", typeof(Document))]
public sealed class DocumentsController : ControllerBase
{
[HttpGet("{id}")]
public IActionResult Get(string id) => Ok();
}
Response Behavior
| Status | Condition |
|---|---|
400 Bad Request |
Route id is missing or empty, or typed key parsing fails (for Register<T, TKey>). |
401 Unauthorized |
User claim is missing, or tenant claim is missing when a tenant-aware descriptor is used. |
403 Forbidden |
Resource exists but ownership or tenant check fails, and HideExistenceWhenForbidden is false. |
404 Not Found |
Resource does not exist, or ownership/tenant check fails when HideExistenceWhenForbidden is true. |
500 Internal Server Error |
Descriptor is not registered for the requested entity type. |
Startup Validation (Fail-Fast)
OwnershipGuard validates ownership descriptors during startup.
- Minimal API endpoints are validated when configured with
.RequireOwnership<TEntity>("id"). - MVC actions are validated when decorated with
[RequireOwnership(...)]. - If a required descriptor is missing, app startup fails with
InvalidOperationExceptionand endpoint details.
Configuration
| Option | Default | Description |
|---|---|---|
UserIdClaimType |
ClaimTypes.NameIdentifier |
Claim type used to resolve the current user id. |
TenantIdClaimType |
"tenant_id" |
Claim type used to resolve the current tenant id when tenant checks are required. |
UseProblemDetailsResponses |
true |
When true, filters return ProblemDetails payloads for errors. When false, filters return plain status responses. |
HideExistenceWhenForbidden |
false |
When true, failed ownership/tenant checks return 404; otherwise they return 403. |
Optional error message overrides (when set, used as ProblemDetails.Title; when null, built-in defaults are used): MissingResourceIdMessage, InvalidResourceIdInRouteMessage, UnauthorizedMessage, TenantNotSpecifiedMessage, InvalidResourceIdMessage, NotFoundMessage, ForbiddenMessage, DescriptorNotRegisteredMessage. Useful for i18n or custom wording.
Optional Query Helpers
using OwnershipGuard.EntityFrameworkCore;
var ownedDocs = await db.Documents
.WhereOwnedBy(userId, d => d.OwnerId)
.ToListAsync();
var tenantDocs = await db.Documents
.WhereTenant(tenantId, d => d.TenantId)
.ToListAsync();
Non-Goals
OwnershipGuard does not implement:
- Authentication
- Authorization policy systems (RBAC)
- Rate limiting
- Input validation
License
MIT. See LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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 was computed. 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. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.11)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Optional error message overrides in OwnershipGuardOptions (i18n/custom wording). RequireOwnershipFilter.For<TEntity>("id") factory for Minimal API. All filters use custom messages when set.