Albatross.Authentication.Windows 9.0.1

Prefix Reserved
This package has a SemVer 2.0.0 package version: 9.0.1+a13e79e.
dotnet add package Albatross.Authentication.Windows --version 9.0.1
                    
NuGet\Install-Package Albatross.Authentication.Windows -Version 9.0.1
                    
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="Albatross.Authentication.Windows" Version="9.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Albatross.Authentication.Windows" Version="9.0.1" />
                    
Directory.Packages.props
<PackageReference Include="Albatross.Authentication.Windows" />
                    
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 Albatross.Authentication.Windows --version 9.0.1
                    
#r "nuget: Albatross.Authentication.Windows, 9.0.1"
                    
#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 Albatross.Authentication.Windows@9.0.1
                    
#: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=Albatross.Authentication.Windows&version=9.0.1
                    
Install as a Cake Addin
#tool nuget:?package=Albatross.Authentication.Windows&version=9.0.1
                    
Install as a Cake Tool

Albatross.Authentication.Windows

A .NET library that provides Windows-specific authentication functionality for retrieving the identity of the current Windows user. This library integrates with the Albatross.Authentication framework to provide seamless Windows authentication support in Windows-hosted applications.

Features

  • Windows Identity Extraction: Retrieves the current Windows user identity using System.Security.Principal.WindowsIdentity.GetCurrent()
  • Domain User Support: Automatically normalizes domain usernames by removing the domain prefix (e.g., DOMAIN\username becomes username)
  • ILogin Implementation: Provides WindowsLogin record that implements the ILogin interface with Provider, Subject, and Name properties
  • Service Implementations:
    • IGetCurrentLogin implementation for getting structured login information
    • IGetCurrentUser implementation for getting the username as a string (legacy support)
  • Dependency Injection: Built-in extension methods for easy service registration
  • Claims-based Authentication: Extracts user information from Windows identity claims (PrimarySid and Name)

Prerequisites

  • .NET SDK: .NET 8.0 or .NET 9.0
  • Target Framework: net8.0-windows or net9.0-windows
  • Operating System: Windows (required for Windows Identity functionality)
  • Dependencies:
    • Microsoft.Extensions.DependencyInjection.Abstractions (v9.0.7)
    • System.Security.Principal.Windows (v5.0.0)
    • Albatross.Authentication (base library)

Installation

1. Install the NuGet Package

dotnet add package Albatross.Authentication.Windows

2. Restore Dependencies

dotnet restore

3. Build the Project

dotnet build

4. Run the Application

dotnet run

Example Usage

Basic Setup with Dependency Injection

using Albatross.Authentication.Windows;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

// Register Windows authentication services
var builder = Host.CreateDefaultBuilder();
builder.ConfigureServices(services =>
{
    services.AddWindowsPrincipalProvider();
});

var host = builder.Build();

Getting Current User Information

using Albatross.Authentication;

// Get the current user as a string (legacy method)
var userService = serviceProvider.GetRequiredService<IGetCurrentUser>();
string currentUser = userService.Get();
Console.WriteLine($"Current user: {currentUser}"); // Output: "john.doe" (without domain)

// Get structured login information
var loginService = serviceProvider.GetRequiredService<IGetCurrentLogin>();
ILogin login = loginService.Get();
Console.WriteLine($"Provider: {login.Provider}"); // Output: "Windows"
Console.WriteLine($"Subject: {login.Subject}");   // Output: User's SID
Console.WriteLine($"Name: {login.Name}");         // Output: "DOMAIN\john.doe"

Creating WindowsLogin Directly

using System.Security.Principal;
using Albatross.Authentication.Windows;

// Get current Windows identity
var identity = WindowsIdentity.GetCurrent();

// Create WindowsLogin instance
var windowsLogin = new WindowsLogin(identity);

Console.WriteLine($"Provider: {windowsLogin.Provider}"); // "Windows"
Console.WriteLine($"Subject: {windowsLogin.Subject}");   // User's SID
Console.WriteLine($"Name: {windowsLogin.Name}");         // Full domain\username

Integration in ASP.NET Core or Windows Services

// In a Windows Service or desktop application
public class MyWorkerService : BackgroundService
{
    private readonly IGetCurrentLogin _getCurrentLogin;

    public MyWorkerService(IGetCurrentLogin getCurrentLogin)
    {
        _getCurrentLogin = getCurrentLogin;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var currentLogin = _getCurrentLogin.Get();
        // Use the login information for auditing, logging, etc.
    }
}

Project Structure

Albatross.Authentication.Windows/
├── Albatross.Authentication.Windows.csproj    # Project file with dependencies and targeting
├── README.md                                  # This documentation file
├── WindowsLogin.cs                           # ILogin implementation for Windows
├── GetCurrentWindowsLogin.cs                 # Service to get ILogin from Windows identity
├── GetCurrentWindowsUser.cs                  # Service to get username string (legacy)
└── Extensions.cs                             # Dependency injection extension methods

Key Components

  • WindowsLogin: A record that implements ILogin interface, extracting user information from Windows identity claims
  • GetCurrentWindowsLogin: Service class that creates WindowsLogin instances from the current Windows identity
  • GetCurrentWindowsUser: Legacy service that returns the current username as a string with domain prefix removed
  • Extensions: Contains AddWindowsPrincipalProvider() method for easy dependency injection setup

How to Run Unit Tests

Prerequisites for Testing

  • Ensure you're running on a Windows machine with a valid Windows user context
  • The tests use Environment.UserName and Environment.UserDomainName for validation

Running Tests

# Navigate to the test project directory
cd Albatross.Authentication.UnitTest

# Run all tests
dotnet test

# Run only Windows authentication tests
dotnet test --filter "TestWindowsAuthentication"

# Run with verbose output
dotnet test --verbosity normal

Example Test Cases

The unit tests validate:

  • User identity extraction matches Environment.UserName
  • Login provider is correctly set to "Windows"
  • Domain and username formatting is correct
  • Claims extraction from Windows identity works properly

Contributing Guidelines

We welcome contributions to improve the Albatross.Authentication.Windows library!

Steps to Contribute

  1. Fork the Repository: Create a fork of the authentication repository

  2. Create a Feature Branch:

    git checkout -b feature/your-feature-name
    
  3. Make Your Changes:

    • Follow existing code style and conventions
    • Add unit tests for new functionality
    • Update documentation as needed
  4. Test Your Changes:

    dotnet test
    
  5. Submit a Pull Request:

    • Provide a clear description of your changes
    • Reference any related issues
    • Ensure all tests pass

Code Standards

  • Follow C# coding conventions
  • Use meaningful variable and method names
  • Add XML documentation comments for public APIs
  • Maintain compatibility with existing interfaces
  • Target the appropriate .NET frameworks (net8.0-windows, net9.0-windows)

Reporting Issues

  • Use the GitHub Issues page
  • Provide detailed reproduction steps
  • Include environment information (OS, .NET version, etc.)

License

This project is licensed under the MIT License. See the LICENSE file for the full license text.

MIT License Summary

  • ✅ Commercial use allowed
  • ✅ Modification allowed
  • ✅ Distribution allowed
  • ✅ Private use allowed
  • ❌ No warranty provided
  • ❌ No liability accepted

Developed and maintained by Rushui Guan

For more information about the broader authentication framework, see the main repository README.

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net9.0-windows7.0 is compatible.  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
9.0.1 37 9/5/2025
9.0.0 39 7/15/2025
7.6.6-26.main 38 5/22/2025
7.6.5 46 5/4/2025
7.6.2 158 2/27/2025
7.6.0 79 12/28/2024
7.5.8 80 11/11/2024
7.5.6 67 11/8/2024
7.5.5 61 11/7/2024
7.5.4 63 11/7/2024
7.4.2 68 10/8/2024
7.2.6 71 5/17/2024