Soenneker.Utils.UserContext 4.0.1782

Prefix Reserved
There is a newer version of this package available.
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
                    
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="4.0.1782" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Utils.UserContext" Version="4.0.1782" />
                    
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 4.0.1782
                    
#r "nuget: Soenneker.Utils.UserContext, 4.0.1782"
                    
#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@4.0.1782
                    
#: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=4.0.1782
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Utils.UserContext&version=4.0.1782
                    
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 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. 
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
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
Loading failed

Update dependency Soenneker.Extensions.String to 4.0.733 (#2501)