ImanSoftware.CurrentUserAccessor 1.0.0

dotnet add package ImanSoftware.CurrentUserAccessor --version 1.0.0
                    
NuGet\Install-Package ImanSoftware.CurrentUserAccessor -Version 1.0.0
                    
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="ImanSoftware.CurrentUserAccessor" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ImanSoftware.CurrentUserAccessor" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ImanSoftware.CurrentUserAccessor" />
                    
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 ImanSoftware.CurrentUserAccessor --version 1.0.0
                    
#r "nuget: ImanSoftware.CurrentUserAccessor, 1.0.0"
                    
#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 ImanSoftware.CurrentUserAccessor@1.0.0
                    
#: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=ImanSoftware.CurrentUserAccessor&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ImanSoftware.CurrentUserAccessor&version=1.0.0
                    
Install as a Cake Tool

ImanSoftware.CurrentUserAccessor

Version: 1.0.0 | Last Updated: August 2026

A lightweight abstraction for accessing the currently authenticated user in ASP.NET Core with support for strongly typed user identifiers.

ImanSoftware.CurrentUserAccessor provides a clean and testable way to access the authenticated user without depending directly on IHttpContextAccessor throughout your application. It supports automatic conversion of the sub claim into strongly typed user IDs such as Guid, int, long, string, and other primitive types.


Installation

Install the package using the .NET CLI:

dotnet add package ImanSoftware.CurrentUserAccessor

Why CurrentUserAccessor?

Accessing HttpContext.User directly spreads infrastructure concerns across your application and makes unit testing more difficult.

Instead of:

var userId = Guid.Parse(
    HttpContext.User.FindFirst("sub")!.Value);

Use:

var userId = _currentUserAccessor.GetCurrentUserId();

This keeps your business logic clean while making it easy to mock the authenticated user during testing.

Benefits include:

  • Strongly typed user identifiers
  • Improved testability
  • Clean Architecture friendly
  • Dependency Injection compatible
  • Centralized user access

Features

  • ICurrentUserAccessor<TUserId>
  • ✅ Strongly typed user IDs
  • ✅ Access to ClaimsPrincipal
  • ✅ Automatic sub claim conversion
  • ✅ Supports Guid, int, long, string, and other primitive types
  • ✅ Built on IHttpContextAccessor
  • ✅ Open Generic Dependency Injection registration
  • ✅ Lightweight and extensible

ICurrentUserAccessor<TUserId>

The primary abstraction for accessing the authenticated user.

Available methods:

  • GetCurrentUserId()
  • GetCurrentUser()

Example:

Guid userId = _currentUserAccessor.GetCurrentUserId();

ClaimsPrincipal user = _currentUserAccessor.GetCurrentUser();

HttpContextUserAccessor<TUserId>

The default implementation uses IHttpContextAccessor to retrieve the current authenticated user.

Features include:

  • Reads the authenticated ClaimsPrincipal
  • Extracts the sub claim
  • Automatically converts it to TUserId
  • Supports custom user identifier types through TypeDescriptor

Supported User ID Types

The package supports any type that can be converted through TypeDescriptor.

Common examples include:

  • Guid
  • string
  • int
  • long
  • short
  • byte
  • Other primitive types

Example:

ICurrentUserAccessor<Guid>

ICurrentUserAccessor<int>

ICurrentUserAccessor<long>

ICurrentUserAccessor<string>

Dependency Injection

Register the accessor in your application:

services.AddCurrentUserAccessor();

The service is registered as an Open Generic, allowing it to be injected using any supported user ID type.

Note

If you are using AddImanFramework(), the current user accessor is registered automatically.


Example

using ImanSoftware.CurrentUserAccessor;

public class ProfileService
{
    private readonly ICurrentUserAccessor<Guid> _userAccessor;

    public ProfileService(
        ICurrentUserAccessor<Guid> userAccessor)
    {
        _userAccessor = userAccessor;
    }

    public void ShowProfile()
    {
        Guid userId = _userAccessor.GetCurrentUserId();

        ClaimsPrincipal user = _userAccessor.GetCurrentUser();

        // Business logic...
    }
}

Unit Testing

Because user access is abstracted behind an interface, it can easily be mocked.

var mock = new Mock<ICurrentUserAccessor<Guid>>();

mock.Setup(x => x.GetCurrentUserId())
    .Returns(Guid.Parse(
        "11111111-1111-1111-1111-111111111111"));

This allows your business logic to be tested without an active HTTP request.


Design Goals

  • Strongly typed API
  • Clean abstraction
  • Dependency Injection friendly
  • Easy to mock
  • Lightweight
  • Framework independent
  • Clean Architecture compatible
  • ASP.NET Core friendly
  • Minimal API

Requirements

  • .NET Standard 2.0+
  • .NET 6+
  • .NET 7+
  • .NET 8+
  • .NET 9+
  • .NET 10.0+

Author

Iman Estiri

📧 contact.imanestiri@gmail.com

📧 dev.imanestiri@gmail.com

GitHub:

https://github.com/ImanEstiri


License

This project is licensed under the MIT License.

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 (1)

Showing the top 1 NuGet packages that depend on ImanSoftware.CurrentUserAccessor:

Package Downloads
ImanSoftware.Framework

Umbrella package that bundles all ImanSoftware modules for a unified development experience.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 157 8/3/2026