Lewee.Infrastructure.Auth 0.40.3

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

Lewee.Infrastructure.Auth

Authentication and authorization infrastructure components for domain-driven design applications. This package provides infrastructure layer services for retrieving authenticated user context and propagating authentication tokens to external HTTP requests.

Dependencies

  • Microsoft.AspNetCore.App (framework reference for IHttpContextAccessor and authentication infrastructure)
  • Lewee.Common - Shared utilities including IAuthenticatedUserService interface

Features

This infrastructure package provides:

  • Authenticated User Service: Retrieves the current user's ID from the authentication context
  • Authentication Token Handler: HTTP message handler that automatically adds Bearer tokens to outgoing HTTP requests

Configuration

Authenticated User Service

Register the authenticated user service to access the current user's ID:

using Lewee.Infrastructure.Auth;

services.AddAuthenticatedUserService();

This registers:

  • IHttpContextAccessor - ASP.NET Core service for accessing the current HTTP context
  • IAuthenticatedUserService - Service for retrieving the authenticated user's ID

Authentication Token Delegating Handler

The AuthTokenDelegatingHandler is typically configured automatically by other infrastructure packages (such as Lewee.Infrastructure.Refit) but can be registered manually if needed:

services.AddTransient<AuthTokenDelegatingHandler>();

Usage

Authenticated User Service

Inject IAuthenticatedUserService into services that need to obtain the UserId of the authenticated user. The user ID is retrieved from the NameIdentifier claim in the authentication context (typically from a JWT token).

using Lewee.Common;

public class MyService
{
    private readonly IAuthenticatedUserService authenticatedUserService;

    public MyService(IAuthenticatedUserService authenticatedUserService)
    {
        this.authenticatedUserService = authenticatedUserService;
    }

    public void DoSomething()
    {
        var userId = this.authenticatedUserService.UserId;
        if (userId is not null)
        {
            // Use the authenticated user's ID
        }
    }
}

Note: Lewee.Infrastructure.Data uses this service to automatically populate the created/modified by user ID on entity table records.

Authentication Token Handler

The AuthTokenDelegatingHandler is used to automatically add Bearer authentication tokens to outgoing HTTP requests. It:

  1. Retrieves the access_token from the current authentication context (HttpContext)
  2. Adds it as a Bearer token in the Authorization header
  3. Logs warnings when tokens are unavailable

This handler is automatically configured by infrastructure packages like Lewee.Infrastructure.Refit.

Infrastructure Components

Component Description
AuthenticatedUserServiceConfiguration.cs Extension method for registering authenticated user service
AuthenticatedUserService.cs Implementation of IAuthenticatedUserService that retrieves user ID from HTTP context
AuthTokenDelegatingHandler.cs HTTP message handler for automatic authentication token propagation

Integration with Lewee DDD Layers

This infrastructure package integrates with other Lewee packages across the DDD layers:

Package Layer Integration
Lewee.Common Cross-cutting Defines IAuthenticatedUserService interface
Lewee.Infrastructure.Data Infrastructure Uses IAuthenticatedUserService for audit tracking
Lewee.Infrastructure.Refit Infrastructure Uses AuthTokenDelegatingHandler for API client authentication

Best Practices

  1. Register Early: Call AddAuthenticatedUserService() during application startup before services that depend on IAuthenticatedUserService
  2. Check for Null: Always check if UserId is null before using it, as unauthenticated requests will return null
  3. Authentication Required: Ensure users are properly authenticated before services attempt to access IAuthenticatedUserService.UserId
  4. HTTP Context Required: Both components require an active HttpContext, which is available in web applications but not in background services or console applications
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 Lewee.Infrastructure.Auth:

Package Downloads
Lewee.Infrastructure.Fluxor

Package Description

Lewee.Infrastructure.Refit

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.40.3 50 11/22/2026