Alva.ServiceKit.IdentityService 1.8.0

There is a newer version of this package available.
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
                    
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="Alva.ServiceKit.IdentityService" Version="1.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Alva.ServiceKit.IdentityService" Version="1.8.0" />
                    
Directory.Packages.props
<PackageReference Include="Alva.ServiceKit.IdentityService" />
                    
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 Alva.ServiceKit.IdentityService --version 1.8.0
                    
#r "nuget: Alva.ServiceKit.IdentityService, 1.8.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 Alva.ServiceKit.IdentityService@1.8.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=Alva.ServiceKit.IdentityService&version=1.8.0
                    
Install as a Cake Addin
#tool nuget:?package=Alva.ServiceKit.IdentityService&version=1.8.0
                    
Install as a Cake Tool

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 / ModifiedBy fields
  • Auditing and change tracking
  • Background jobs and system processes
  • Unified user access abstraction across web and non-web contexts

License

MIT

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.1 138 6/26/2026
3.1.0 121 6/26/2026
1.8.0 222 12/24/2025
1.0.2 435 2/27/2023
1.0.0 374 2/26/2023