Soenneker.Utils.UserContext
4.0.1782
Prefix Reserved
See the version list below for details.
dotnet add package Soenneker.Utils.UserContext --version 4.0.1782
NuGet\Install-Package Soenneker.Utils.UserContext -Version 4.0.1782
<PackageReference Include="Soenneker.Utils.UserContext" Version="4.0.1782" />
<PackageVersion Include="Soenneker.Utils.UserContext" Version="4.0.1782" />
<PackageReference Include="Soenneker.Utils.UserContext" />
paket add Soenneker.Utils.UserContext --version 4.0.1782
#r "nuget: Soenneker.Utils.UserContext, 4.0.1782"
#:package Soenneker.Utils.UserContext@4.0.1782
#addin nuget:?package=Soenneker.Utils.UserContext&version=4.0.1782
#tool nuget:?package=Soenneker.Utils.UserContext&version=4.0.1782
Soenneker.Utils.UserContext
Scoped access to request identity claims, authorization tokens, API keys, and role checks, with explicit internal-context overrides.
Installation
dotnet add package Soenneker.Utils.UserContext
Registration
using Soenneker.Utils.UserContext.Registrars;
services.AddUserContextAsScoped();
Then inject IUserContext wherever you need it.
IUserContext is intentionally scoped. It caches resolved values for the lifetime of the scope and assumes one request identity per scope; do not register it as a singleton or reuse a scope across unrelated requests.
User identity
string userId = userContext.GetId();
string? optionalUserId = userContext.GetIdSafe();
string email = userContext.GetEmail();
User ID claims are checked in this order:
http://schemas.microsoft.com/identity/claims/objectidentifieroidClaimTypes.NameIdentifiersub
GetId throws UnauthorizedException when no non-empty supported claim is available; GetIdSafe returns null. GetEmail first checks ClaimTypes.Email, then the emails claim, and throws UnauthorizedException when neither contains a value.
Resolved IDs and email addresses are cached. Changing HttpContextAccessor.HttpContext afterward does not invalidate them.
Roles
bool canManage = userContext.HasRole("Manager");
bool isBothAdminAndManager = userContext.HasRoles("Admin", "Manager");
bool isAdmin = userContext.IsAdmin();
HasRoles requires every supplied role; it is not an “any role” check. It returns false when there is no current user. With a current user, an empty role array returns true. HasRole and HasRoles delegate matching to the current ClaimsPrincipal.
IsAdmin checks the Admin role once and caches the result. IsNotAdmin negates that cached result.
Authorization token and API key
string token = userContext.GetJwt();
string? apiKey = userContext.GetApiKey();
GetJwt parses the first Authorization header value and returns its parameter. It does not require the scheme to be Bearer; missing, malformed, or parameterless values throw UnauthorizedException. The extracted value is cached without validating the token.
GetApiKey reads the X-Api-Key header defined by Soenneker.Constants.Auth, or returns null when unavailable. SetApiKey supplies an override for the current context instance; blank overrides throw ArgumentException.
Internal operations
userContext.SetInternalContext("service.example");
This overrides the cached user ID with Guid.Empty, the cached email with internal@service.example, and the cached admin result with true. It does not create an HTTP principal, add roles, set a JWT, or set an API key, so HasRole and HasRoles still inspect the request principal.
The internal-context domain is interpolated as supplied and is not validated as a DNS or email domain.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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
- Soenneker.Constants.Auth (>= 4.0.619)
- Soenneker.Exceptions.Suite (>= 4.0.691)
- Soenneker.Extensions.String (>= 4.0.733)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Soenneker.Utils.UserContext:
| Package | Downloads |
|---|---|
|
Soenneker.Cosmos.Repository
A data persistence abstraction layer for Cosmos DB |
|
|
Soenneker.Managers.Base
An abstract class providing common functionality for managers |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 4.0.1785 | 349 | 8/31/2026 |
| 4.0.1784 | 143 | 8/31/2026 |
| 4.0.1783 | 174 | 8/31/2026 |
| 4.0.1782 | 133 | 8/31/2026 |
| 4.0.1781 | 79 | 8/30/2026 |
| 4.0.1780 | 227 | 8/30/2026 |
| 4.0.1779 | 249 | 8/30/2026 |
| 4.0.1778 | 204 | 8/30/2026 |
| 4.0.1776 | 142 | 8/30/2026 |
| 4.0.1775 | 113 | 8/29/2026 |
| 4.0.1774 | 80 | 8/29/2026 |
| 4.0.1773 | 176 | 8/29/2026 |
| 4.0.1772 | 139 | 8/29/2026 |
| 4.0.1771 | 559 | 8/26/2026 |
| 4.0.1770 | 266 | 8/25/2026 |
| 4.0.1769 | 525 | 8/21/2026 |
| 4.0.1768 | 891 | 8/18/2026 |
| 4.0.1767 | 86 | 8/18/2026 |
| 4.0.1766 | 714 | 8/12/2026 |
| 4.0.1765 | 794 | 8/8/2026 |
Update dependency Soenneker.Extensions.String to 4.0.733 (#2501)