Alva.ServiceKit.IdentityService
1.8.0
See the version list below for details.
dotnet add package Alva.ServiceKit.IdentityService --version 1.8.0
NuGet\Install-Package Alva.ServiceKit.IdentityService -Version 1.8.0
<PackageReference Include="Alva.ServiceKit.IdentityService" Version="1.8.0" />
<PackageVersion Include="Alva.ServiceKit.IdentityService" Version="1.8.0" />
<PackageReference Include="Alva.ServiceKit.IdentityService" />
paket add Alva.ServiceKit.IdentityService --version 1.8.0
#r "nuget: Alva.ServiceKit.IdentityService, 1.8.0"
#:package Alva.ServiceKit.IdentityService@1.8.0
#addin nuget:?package=Alva.ServiceKit.IdentityService&version=1.8.0
#tool nuget:?package=Alva.ServiceKit.IdentityService&version=1.8.0
ServiceKit.IdentityService
Lightweight helper interface and implementations for accessing information about the currently authenticated user.
This package is part of the shared helper library set: Alva.ServiceKit.*
Designed for use in ASP.NET Core applications, including background services and Entity Framework Core contexts.
Installation
Install via NuGet:
.NET CLI
dotnet add package Alva.ServiceKit.IdentityService
PMC
NuGet\Install-Package Alva.ServiceKit.IdentityServic
Usage
Registering the service
Program.cs
builder.Services.AddTransient<IUserService, UserService>();
For background jobs or system-level operations, you may use the admin implementation:
builder.Services.AddTransient<IUserService>(
_ => new UserServiceAdmin("admin")
);
Using with Entity Framework Core
AppDbContext.cs
public class AppDbContext : DbContext
{
private readonly IUserService? _userService;
public AppDbContext(
DbContextOptions<AppDbContext> options,
IUserService? userService = null)
: base(options)
{
_userService = userService;
}
private void UpdateBaseEditedEntity()
{
var currentUser = _userService?.GetCurrentUserName();
foreach (EntityEntry<IBaseEditedEntity> entry
in ChangeTracker.Entries<IBaseEditedEntity>())
{
switch (entry.State)
{
case EntityState.Added:
entry.Entity.CreatedBy = currentUser;
entry.Entity.Created = DateTime.Now;
entry.Entity.ModifiedBy = currentUser;
entry.Entity.Modified = DateTime.Now;
break;
case EntityState.Modified:
entry.Entity.ModifiedBy = currentUser;
entry.Entity.Modified = DateTime.Now;
break;
}
}
}
}
Interface
public interface IUserService
{
string? GetCurrentUserName();
ClaimsPrincipal? GetCurrentUser();
}
Implementations
UserService
Retrieves user information from the current HTTP context.
public string? GetCurrentUserName()
{
return _context.HttpContext?.User?.Identity?.Name;
}
public ClaimsPrincipal? GetCurrentUser()
{
return _context.HttpContext?.User;
}
UserServiceAdmin
System-level implementation for background tasks, migrations, or administrative actions.
public UserServiceAdmin(string? username = "admin")
{
_username = username ?? "admin";
}
public string GetCurrentUserName()
{
return _username;
}
public ClaimsPrincipal GetCurrentUser()
{
return new ClaimsPrincipal();
}
Typical Use Cases
- Automatic population of
CreatedBy/ModifiedByfields - Auditing and change tracking
- Background jobs and system processes
- Unified user access abstraction across web and non-web contexts
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.