Sprint.Shared.Authentication 1.0.0

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

Shared.Authentication

JWT Bearer authentication library designed for the Sprint ecosystem (.NET 10+). Handles JWT validation, distributed cache session lookup, role-based access control, and populates a scoped CurrentUser context object — all wired up with a single AddSharedAuthentication call.


Key Features

  • JWT Validation: Validates tokens using RSA Public Key via Microsoft.AspNetCore.Authentication.JwtBearer.
  • Session Cache (Optional): Caches resolved Actor data in IDistributedCache (Redis, Memory, etc.) to reduce Auth Service round-trips. Gracefully degrades if no cache is registered.
  • Role-Based Access Control: Validates that the actor holds a permitted role via IClaimManagementService before the request proceeds.
  • Scoped CurrentUser: Populates a DI-injectable CurrentUser object with identity and claim data (company, role, application, vendor) on every validated request.
  • Token Expiry Header: Automatically sets Token-Expired: true response header when a token has expired.
  • Requires .NET 10.0+

Installation & Setup

1. Register Services in Program.cs

using Shared.Authentication.Extensions;

// Option 1: Automatic from Configuration
builder.Services.AddSharedAuthentication(builder.Configuration);

// Option 2: Manual setup via Action
builder.Services.AddSharedAuthentication(options => {
    options.JwtSettings = new Jwt {
        PublicKey              = "-----BEGIN PUBLIC KEY-----...",
        GetSessionBySessionIdUrl = "https://auth-service/api/session/"
    };
    options.IsCacheEnabled    = true;
    options.CacheDurationInSec = 900; // 15 minutes
});

2. Enable Middleware in Program.cs

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.Run();

3. Configuration Template (appsettings.json)

{
  "Jwt": {
    "PublicKey": "-----BEGIN PUBLIC KEY-----\nMIIB...\n-----END PUBLIC KEY-----",
    "GetSessionBySessionIdUrl": "https://auth-service.internal/api/session/"
  },
  "CacheSetting": {
    "Token": {
      "Enabled": true,
      "DurationInSec": 900
    }
  }
}

Using CurrentUser in Controllers / Services

Inject CurrentUser directly — it is populated automatically after JWT validation:

public class OrderEndpoint : Endpoint<OrderRequest>
{
    private readonly CurrentUser _currentUser;

    public OrderEndpoint(CurrentUser currentUser)
    {
        _currentUser = currentUser;
    }

    public override async Task HandleAsync(OrderRequest req, CancellationToken ct)
    {
        var userId   = _currentUser.Id;
        var userName = _currentUser.Name;
        var roles    = _currentUser.ClaimRoleIds;   // comma-separated role IDs
        var companies = _currentUser.ClaimCompanyCodes; // comma-separated company codes

        await SendOkAsync(ct);
    }
}

CurrentUser Properties

Property Type Description
Id string? Actor ID from session
Name string? Actor display name
SessionId string? Session identifier from JWT claim
ClaimCompanyIds string? Comma-separated company IDs
ClaimCompanyCodes string? Comma-separated company codes
ClaimCompanyNames string? Comma-separated company names
ClaimRoleIds string? Comma-separated role IDs
ClaimRoleCodes string? Comma-separated role codes
ClaimRoleNames string? Comma-separated role names
ClaimApplicationIds string? Comma-separated application IDs
ClaimApplicationCodes string? Comma-separated application codes
ClaimApplicationNames string? Comma-separated application names
ClaimVendorCode string? Vendor code (nullable)
ClaimCompanyBranch string? Company branch (nullable)

Helper: IsValidUserId

Checks if the given userId matches the current actor, or if the actor has admin role (Role ID 4):

if (!_currentUser.IsValidUserId(req.TargetUserId))
{
    await SendForbiddenAsync(ct);
    return;
}

Optional: Distributed Cache (Redis)

When IsCacheEnabled = true, the library will use IDistributedCache if one is registered. If none is registered, it falls back to calling the Auth Service on every request without crashing.

// Register Redis cache before AddSharedAuthentication
builder.Services.AddStackExchangeRedisCache(options => {
    options.Configuration = builder.Configuration.GetConnectionString("Redis");
});

builder.Services.AddSharedAuthentication(builder.Configuration);

Dependencies

Package Version
Microsoft.AspNetCore.Authentication.JwtBearer 10.0.1
Utility.Package 10.0.2

© 2026 Sprint-OMS Development Team

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
1.0.0 164 4/20/2026