Soenneker.Utils.UserContext 11.0.1861

Prefix Reserved
dotnet add package Soenneker.Utils.UserContext --version 11.0.1861
                    
NuGet\Install-Package Soenneker.Utils.UserContext -Version 11.0.1861
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Utils.UserContext" Version="11.0.1861" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.UserContext" Version="11.0.1861" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Utils.UserContext" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Utils.UserContext --version 11.0.1861
                    
#r "nuget: Soenneker.Utils.UserContext, 11.0.1861"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Utils.UserContext@11.0.1861
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Utils.UserContext&version=11.0.1861
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.UserContext&version=11.0.1861
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image 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:

  1. http://schemas.microsoft.com/identity/claims/objectidentifier
  2. oid
  3. ClaimTypes.NameIdentifier
  4. sub

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 Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
11.0.1861 385 11/11/2026
11.0.1860 149 11/11/2026
11.0.1859 139 11/11/2026
11.0.1858 143 11/11/2026
11.0.1857 51 11/11/2026
11.0.1855 53 11/11/2026
11.0.1854 316 11/11/2026
11.0.1853 1,510 11/3/2026
11.0.1852 164 11/3/2026
11.0.1851 374 11/2/2026
11.0.1850 1,293 10/27/2026
11.0.1849 120 10/27/2026
11.0.1848 501 10/26/2026
11.0.1847 253 10/26/2026
11.0.1846 358 10/26/2026
11.0.1845 110 10/26/2026
11.0.1844 99 10/26/2026
4.0.1843 1,285 10/15/2026
4.0.1842 120 10/14/2026
4.0.1841 189 10/14/2026
Loading failed

Updated Soenneker.Extensions.String to 11.0.820