RCommon.Security
2.4.1
dotnet add package RCommon.Security --version 2.4.1
NuGet\Install-Package RCommon.Security -Version 2.4.1
<PackageReference Include="RCommon.Security" Version="2.4.1" />
<PackageVersion Include="RCommon.Security" Version="2.4.1" />
<PackageReference Include="RCommon.Security" />
paket add RCommon.Security --version 2.4.1
#r "nuget: RCommon.Security, 2.4.1"
#:package RCommon.Security@2.4.1
#addin nuget:?package=RCommon.Security&version=2.4.1
#tool nuget:?package=RCommon.Security&version=2.4.1
RCommon.Security
Provides claims-based security abstractions for RCommon, including current user and current client identity access built on top of ClaimsPrincipal, with configurable claim type mappings and multi-tenancy support.
Features
- Current user abstraction --
ICurrentUserexposes the authenticated user's ID, tenant ID, roles, and claims without depending on a specific auth framework - Current client abstraction --
ICurrentClientidentifies the calling OAuth/API client from theclient_idclaim - ClaimsPrincipal accessor --
ICurrentPrincipalAccessorprovides the current principal with support for temporary principal replacement via scopedIDisposable - AsyncLocal principal override --
CurrentPrincipalAccessorBaseusesAsyncLocal<T>so overridden principals flow across async contexts - Configurable claim types --
ClaimTypesConstallows customizing which claim URIs map to user ID, tenant ID, client ID, roles, etc. - ClaimsIdentity extensions -- helper methods for finding user/tenant/client IDs and for safely adding or replacing claims
- Tenant ID accessor --
ITenantIdAccessorprovides runtime access to the current tenant ID for repository filtering;ClaimsTenantIdAccessorresolves it from claims,NullTenantIdAccessoris the default no-op - Authorization exception --
AuthorizationExceptionwith configurable severity, error codes, and fluent data attachment - Fluent builder API -- integrates with the
AddRCommon()builder pattern for one-line DI registration
Installation
dotnet add package RCommon.Security
Usage
using RCommon;
using RCommon.Security.Users;
using RCommon.Security.Clients;
// Register security services in your DI setup
services.AddRCommon(config =>
{
config.WithClaimsAndPrincipalAccessor();
});
// Inject ICurrentUser or ICurrentClient in your services
public class TenantService
{
private readonly ICurrentUser _currentUser;
private readonly ICurrentClient _currentClient;
public TenantService(ICurrentUser currentUser, ICurrentClient currentClient)
{
_currentUser = currentUser;
_currentClient = currentClient;
}
public string GetTenantId()
{
if (!_currentUser.IsAuthenticated)
throw new UnauthorizedAccessException("User is not authenticated.");
return _currentUser.TenantId
?? throw new InvalidOperationException("No tenant claim found.");
}
public string GetClientId()
{
return _currentClient.Id
?? throw new InvalidOperationException("No client identity found.");
}
public bool IsInRole(string role)
{
return _currentUser.Roles.Contains(role);
}
}
Tenant ID Accessor
ITenantIdAccessor provides runtime access to the current tenant ID. When WithClaimsAndPrincipalAccessor() is called, the ClaimsTenantIdAccessor is registered, which resolves the tenant ID from the authenticated user's claims:
using RCommon.Security.Claims;
// ITenantIdAccessor is used by repositories to automatically filter and stamp entities
public class TenantAwareService
{
private readonly ITenantIdAccessor _tenantIdAccessor;
public TenantAwareService(ITenantIdAccessor tenantIdAccessor)
{
_tenantIdAccessor = tenantIdAccessor;
}
public string? GetCurrentTenant()
{
return _tenantIdAccessor.GetTenantId();
}
}
The default NullTenantIdAccessor returns null, which causes all tenant filtering to be bypassed. This allows the application to operate without multitenancy configured.
Customizing Claim Types
// Override the default claim type URIs at startup if your identity provider uses custom claims
ClaimTypesConst.UserId = "sub";
ClaimTypesConst.TenantId = "tenant";
ClaimTypesConst.ClientId = "azp";
Key Types
| Type | Description |
|---|---|
ICurrentUser |
Provides the authenticated user's ID, tenant ID, roles, and claim lookups |
CurrentUser |
Default implementation that reads from the current ClaimsPrincipal |
ICurrentClient |
Provides the authenticated client application's ID and authentication status |
CurrentClient |
Default implementation that reads the client_id claim from the principal |
ICurrentPrincipalAccessor |
Accesses the current ClaimsPrincipal and supports scoped replacement |
ThreadCurrentPrincipalAccessor |
Default accessor that reads from Thread.CurrentPrincipal |
CurrentPrincipalAccessorBase |
Abstract base using AsyncLocal<T> for async-safe principal overrides |
ClaimTypesConst |
Configurable constants for standard claim type URIs (user ID, role, tenant, etc.) |
AuthorizationException |
Exception for unauthorized requests with log level, error code, and fluent data API |
ClaimsIdentityExtensions |
Extension methods for extracting user/tenant/client IDs and managing claims |
ITenantIdAccessor |
Runtime accessor returning the current tenant ID (string?) for repository filtering |
ClaimsTenantIdAccessor |
Claims-based implementation resolving tenant ID from ICurrentPrincipalAccessor |
NullTenantIdAccessor |
Default no-op implementation returning null (tenant filtering bypassed) |
Documentation
For full documentation, visit rcommon.com.
Related Packages
- RCommon.Core - Core abstractions and builder infrastructure
- RCommon.Web - ASP.NET Core integration with
HttpContextCurrentPrincipalAccessor
License
Licensed under the Apache License, Version 2.0.
| 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 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
- RCommon.Core (>= 2.4.1)
-
net8.0
- RCommon.Core (>= 2.4.1)
-
net9.0
- RCommon.Core (>= 2.4.1)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on RCommon.Security:
| Package | Downloads |
|---|---|
|
RCommon.Persistence
A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling, persistence, unit of work, mediator, distributed messaging, event bus, CQRS, email, and more |
|
|
RCommon.Web
A cohesive set of infrastructure libraries for dotnet that utilizes abstractions for event handling, persistence, unit of work, mediator, distributed messaging, event bus, CQRS, email, and more |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.4.1 | 23 | 2/18/2026 |
| 2.3.2-alpha.0.3 | 21 | 2/18/2026 |
| 2.3.2-alpha.0.2 | 23 | 2/18/2026 |
| 2.3.2-alpha.0.1 | 42 | 2/9/2026 |
| 2.3.1 | 86 | 2/5/2026 |
| 2.3.0 | 83 | 2/3/2026 |
| 2.2.2-alpha.0.1 | 382 | 12/11/2025 |
| 2.2.1-alpha.0.2 | 127 | 10/24/2025 |
| 2.2.1-alpha.0.1 | 119 | 10/24/2025 |
| 2.1.11-alpha.0.2 | 123 | 10/24/2025 |
| 2.1.11-alpha.0.1 | 92 | 7/18/2025 |
| 2.1.10 | 187 | 7/17/2025 |
| 2.1.9-alpha.0.1 | 136 | 7/17/2025 |
| 2.1.2.4 | 204 | 5/21/2025 |
| 2.1.2.3 | 196 | 5/1/2025 |
| 2.1.2.2 | 150 | 1/23/2025 |
| 2.1.2.1 | 145 | 1/17/2025 |
| 2.1.2 | 152 | 1/17/2025 |
| 2.1.1.4 | 145 | 1/7/2025 |
| 0.0.0-alpha.0 | 139 | 7/17/2025 |