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
<PackageReference Include="Lewee.Infrastructure.Auth" Version="0.40.3" />
<PackageVersion Include="Lewee.Infrastructure.Auth" Version="0.40.3" />
<PackageReference Include="Lewee.Infrastructure.Auth" />
paket add Lewee.Infrastructure.Auth --version 0.40.3
#r "nuget: Lewee.Infrastructure.Auth, 0.40.3"
#:package Lewee.Infrastructure.Auth@0.40.3
#addin nuget:?package=Lewee.Infrastructure.Auth&version=0.40.3
#tool nuget:?package=Lewee.Infrastructure.Auth&version=0.40.3
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
IHttpContextAccessorand authentication infrastructure) - Lewee.Common - Shared utilities including
IAuthenticatedUserServiceinterface
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 contextIAuthenticatedUserService- 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:
- Retrieves the
access_tokenfrom the current authentication context (HttpContext) - Adds it as a
Bearertoken in theAuthorizationheader - 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
- Register Early: Call
AddAuthenticatedUserService()during application startup before services that depend onIAuthenticatedUserService - Check for Null: Always check if
UserIdis null before using it, as unauthenticated requests will return null - Authentication Required: Ensure users are properly authenticated before services attempt to access
IAuthenticatedUserService.UserId - HTTP Context Required: Both components require an active
HttpContext, which is available in web applications but not in background services or console applications
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Lewee.Common (>= 0.40.3)
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 |