SitecoreAPIAuthorization 1.0.0

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

Sitecore API Authorization

GitHub Workflow Status License .NET NuGet

A high-performance, thread-safe .NET library for managing Sitecore Cloud authentication tokens with automatic caching, cleanup, and comprehensive integration testing.

Quick Start

// 1. Install the package
dotnet add package SitecoreAPIAuthorization

// 2. Register services
services.AddLogging(b => b.SetMinimumLevel(LogLevel.Debug).AddConsole());
services.AddSitecoreAuthentication();

// 3. Use in your code
public class MyService
{
    private readonly ISitecoreTokenService _tokenService;
    
    public MyService(ISitecoreTokenService tokenService)
    {
        _tokenService = tokenService;
    }
    
    public async Task<SitecoreAuthToken> GetTokenAsync()
    {
        var credentials = new SitecoreAuthClientCredentials("client-id", "client-secret");
        return await _tokenService.GetSitecoreAuthToken(credentials);
    }
}

Key Features

  • Multi-targeted for .NET 8, .NET 9, and .NET 10
  • Thread-safe token caching (ConcurrentDictionary + bounded PriorityQueue eviction + atomic KeyValuePair removal)
  • Automatic token refresh, expiration handling, and CancellationToken support
  • Built-in HTTP resilience configuration via Microsoft.Extensions.Http.Resilience
  • Secret and bearer token masking in ToString() with zero raw OAuth body logging
  • Dependency injection integration (Scoped and Singleton lifetimes)
  • Logging via Microsoft.Extensions.Logging with structured debug and information messages
  • Comprehensive unit, mock OAuth2, and Docker Keycloak integration tests

Benchmarks

Measured with BenchmarkDotNet against SitecoreTokenCache populated with 64 active entries (MaxCacheSize = 64 for steady-state eviction):

BenchmarkDotNet v0.15.8, macOS 27.0.1 (26A434) [Darwin 27.0.0]
Apple M1 Max, 1 CPU, 10 logical and 10 physical cores
.NET SDK 10.0.401
  .NET 8.0  : .NET 8.0.25 (8.0.2526.11203), Arm64 RyuJIT armv8.0-a
  .NET 9.0  : .NET 9.0.14 (9.0.1426.11910), Arm64 RyuJIT armv8.0-a
  .NET 10.0 : .NET 10.0.12 (10.0.1226.42308), Arm64 RyuJIT armv8.0-a
Method Runtime Mean Error StdDev Gen0 Allocated
GetToken (Cache Hit) .NET 8.0 35.99 ns 0.155 ns 0.129 ns - 0 B
GetToken (Cache Hit) .NET 9.0 33.16 ns 0.153 ns 0.135 ns - 0 B
GetToken (Cache Hit) .NET 10.0 34.98 ns 0.146 ns 0.130 ns - 0 B
SetToken (Update Existing) .NET 8.0 298.02 ns 1.432 ns 1.196 ns 0.0277 176 B
SetToken (Update Existing) .NET 9.0 301.79 ns 2.100 ns 1.754 ns 0.0277 176 B
SetToken (Update Existing) .NET 10.0 296.61 ns 1.524 ns 1.425 ns 0.0176 112 B
RemoveToken (Set + Atomic Remove) .NET 8.0 2,200.36 ns 10.811 ns 9.583 ns 0.0420 264 B
RemoveToken (Set + Atomic Remove) .NET 9.0 3,109.51 ns 14.551 ns 12.899 ns 0.0420 264 B
RemoveToken (Set + Atomic Remove) .NET 10.0 492.74 ns 2.210 ns 2.067 ns 0.0172 112 B
PerformCleanup (64 Entries) .NET 8.0 3,525.64 ns 19.202 ns 17.961 ns 0.0114 88 B
PerformCleanup (64 Entries) .NET 9.0 3,518.24 ns 9.872 ns 9.234 ns 0.0114 88 B
PerformCleanup (64 Entries) .NET 10.0 1,322.99 ns 13.985 ns 16.649 ns - 0 B
SetToken (MaxCacheSize Eviction) .NET 8.0 4,983.31 ns 25.776 ns 22.850 ns 0.0610 408 B
SetToken (MaxCacheSize Eviction) .NET 9.0 4,853.63 ns 22.772 ns 20.187 ns 0.0610 408 B
SetToken (MaxCacheSize Eviction) .NET 10.0 2,091.84 ns 14.582 ns 13.640 ns 0.0381 256 B

Run the benchmark suite locally:

dotnet run --configuration Release --framework net10.0 --project tests/Sitecore.API.Foundation.Authorization.Benchmarks

Logging

The library emits detailed logs that help diagnose authentication and caching behavior.

Service logs:

  • Token cache hit for clientId {ClientId}.
  • Requesting new token for clientId {ClientId} from {AuthUrl}.
  • Auth request payload and response status.
  • Authentication request failed with status {StatusCode} for {AuthUrl}.
  • Failed to parse authentication response for clientId {ClientId}.
  • Authentication response was empty or missing access_token for clientId {ClientId}.
  • Token acquired and cached until {Expiration} for clientId {ClientId}.

Cache logs:

  • Cache miss for clientId {ClientId}.
  • Cache hit for clientId {ClientId}.
  • Token cached for clientId {ClientId} until {Expiration}.
  • Evicted {Count} token(s) due to cache size limit.
  • Cleanup removed {Count} expired token(s).
  • Cache cleared, removed {Count} token(s).
  • Removed token for clientId {ClientId} from cache.

Enable console logging at Debug level in development to see these details.

Quick Commands

# Run mock and infrastructure integration tests
dotnet test tests/Sitecore.API.Foundation.Authorization.IntegrationTests --filter "MockIntegrationTests|InfrastructureTests"

# Run logging integration tests only
dotnet test tests/Sitecore.API.Foundation.Authorization.IntegrationTests --filter "FullyQualifiedName~LoggingIntegrationTests"

# Run all unit and non-Docker integration tests
dotnet test --filter "FullyQualifiedName!~TokenServiceIntegrationTests"

Documentation

For advanced configuration, CancellationToken usage, Microsoft.Extensions.Http.Resilience policies, multi-tenant patterns, and Docker Keycloak test setup, see the Extended Developer & Configuration Guide.

License

MIT License - see LICENSE file for details.

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 is compatible.  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 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 (1)

Showing the top 1 NuGet packages that depend on SitecoreAPIAuthorization:

Package Downloads
SitecoreAPIGraphQLClient

DI-friendly, thread-safe GraphQL client factory for Sitecore with per-client OAuth2 authentication, 401 retry support, and optional HTTP resilience.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 110 11/9/2026
0.9.2 225 8/15/2025
0.9.1 416 8/15/2025
0.9.0 242 8/12/2025

See CHANGELOG.md for release notes.