CG.Infrastructure.Authorization 3.10.2

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

Infrastructure.Authorization

A comprehensive authorization library for .NET applications, providing IdentityServer4 integration with database persistence, configuration management, and operational store capabilities.

Overview

This library provides a complete authorization infrastructure that integrates with IdentityServer4, offering database persistence for clients, API resources, API scopes, and identity resources. It includes comprehensive configuration management, operational store options, and a robust service layer for authorization data management.

Features

  • IdentityServer4 Integration: Full support for IdentityServer4 authorization server
  • Database Persistence: Entity Framework Core integration with SQL Server
  • Configuration Management: Flexible configuration options for authorization data
  • Operational Store: Token cleanup and operational data management
  • CORS Policy Management: Built-in CORS origin validation
  • Comprehensive Testing: Full test coverage with 176+ unit tests
  • Logging Integration: Structured logging throughout all operations
  • Dependency Injection: Native .NET dependency injection support

Project Structure

Infrastructure.Authorization/
├── Common/
│   ├── Entities/           # Shared entity models
│   └── Interfaces/         # Core interfaces
├── Configuration/          # Configuration classes
├── Data/
│   ├── Entities/          # Database entities
│   ├── Queries/           # Database queries
│   └── Repositories/      # Data access layer
├── Extensions/            # Service collection extensions
├── Mappings/              # AutoMapper profiles
├── Options/               # Configuration options
├── Providers/             # Data providers
├── Services/              # Business logic services
└── Stores/                # IdentityServer stores

Core Components

Configuration

  • AuthorizationConfig: Main configuration class for authorization data

    • Clients configuration
    • API Resources configuration
    • API Scopes configuration
    • Identity Resources configuration
  • OperationalStoreOptions: Options for operational store configuration

    • Token cleanup settings
    • Database connection options

Data Layer

  • AuthorizationQueries: Consolidated query class containing all database operations

    • Client queries (CRUD operations)
    • API Resource queries
    • API Scope queries
    • Identity Resource queries
    • CORS origin queries
  • Entities: Complete database entity models

    • Clients: Client configuration storage
    • ClientGrantTypes: Client grant type associations
    • ClientRedirectUris: Client redirect URI storage
    • ClientScopes: Client scope associations
    • ApiResources: API resource definitions
    • ApiScopes: API scope definitions
    • IdentityResources: Identity resource definitions

Providers

  • ClientProvider: Client data access and management

    • Client retrieval by ID
    • Client creation
    • CORS origin querying
    • Comprehensive logging
  • ApiResourceProvider: API resource management

  • ApiScopeProvider: API scope management

  • IdentityResourceProvider: Identity resource management

Services

  • AuthorizationDatabaseService: Core service for authorization data management

    • Ensures authorization data consistency
    • Manages data synchronization
    • Handles database operations
  • CorsPolicyService: CORS policy validation and management

  • TokenCleanupService: Operational store cleanup operations

  • TokenCleanupHostService: Background service for token cleanup

Stores

  • ClientStore: IdentityServer4 client store implementation
  • ResourceStore: IdentityServer4 resource store implementation
  • OperationalStore: IdentityServer4 operational store implementation

Extensions

  • ServiceCollectionExtensions: Dependency injection configuration
    • Service registration
    • Configuration binding
    • Options setup

Installation

dotnet add package Infrastructure.Authorization

Configuration

Basic Setup

// Program.cs or Startup.cs
services.AddAuthorizationInfrastructure(configuration);

Configuration Options

{
  "Authorization": {
    "Clients": [
      {
        "ClientId": "client1",
        "ClientName": "Test Client",
        "GrantTypes": ["client_credentials"],
        "RedirectUris": ["https://localhost:5001/callback"],
        "Scopes": ["api1"]
      }
    ],
    "ApiResources": [
      {
        "Name": "api1",
        "DisplayName": "Test API"
      }
    ],
    "ApiScopes": [
      {
        "Name": "api1",
        "DisplayName": "Test API Scope"
      }
    ],
    "IdentityResources": [
      {
        "Name": "openid",
        "DisplayName": "OpenID"
      }
    ]
  },
  "OperationalStore": {
    "TokenCleanupInterval": "00:15:00",
    "TokenCleanupBatchSize": 100
  }
}

Usage Examples

Client Management

public class ClientService
{
    private readonly IClientProvider _clientProvider;

    public ClientService(IClientProvider clientProvider)
    {
        _clientProvider = clientProvider;
    }

    public async Task<Client?> GetClientAsync(string clientId)
    {
        return await _clientProvider.GetClientById(clientId);
    }

    public async Task CreateClientAsync(Client client)
    {
        await _clientProvider.CreateClient(client);
    }
}

CORS Policy Management

public class CorsService
{
    private readonly ICorsPolicyService _corsPolicyService;

    public CorsService(ICorsPolicyService corsPolicyService)
    {
        _corsPolicyService = corsPolicyService;
    }

    public async Task<bool> IsOriginAllowedAsync(string origin)
    {
        return await _corsPolicyService.IsOriginAllowedAsync(origin);
    }
}

Authorization Data Management

public class AuthorizationService
{
    private readonly IAuthorizationDatabaseService _authService;

    public AuthorizationService(IAuthorizationDatabaseService authService)
    {
        _authService = authService;
    }

    public async Task EnsureAuthorizationDataAsync()
    {
        await _authService.EnsureAuthorizationDataAsync();
    }
}

Testing

The library includes comprehensive unit tests with 176+ test cases covering:

  • Service layer functionality
  • Provider implementations
  • Store implementations
  • Configuration validation
  • Error handling scenarios
  • Edge cases and null handling

Run tests with:

dotnet test

Dependencies

  • .NET 8.0 or later
  • IdentityServer4 - Authorization server framework
  • Entity Framework Core - Database access
  • AutoMapper - Object mapping
  • Microsoft.Extensions.Logging - Logging infrastructure
  • Microsoft.Extensions.Options - Configuration options
  • Microsoft.Extensions.DependencyInjection - Dependency injection

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add or update tests
  5. Ensure all tests pass
  6. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

For issues, questions, or contributions, please use the project's issue tracker or contact the development team.

Product Compatible and additional computed target framework versions.
.NET 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 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 (1)

Showing the top 1 NuGet packages that depend on CG.Infrastructure.Authorization:

Package Downloads
CG.Infrastructure.Identity

Infra Identity library with Duende setup, extensions and database contexts

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.10.2 49 9/1/2025
3.10.1 131 8/21/2025
3.10.0 127 8/21/2025
3.9.1 130 2/26/2025
3.9.0 136 2/20/2025