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
<PackageReference Include="CG.Infrastructure.Authorization" Version="3.10.2" />
<PackageVersion Include="CG.Infrastructure.Authorization" Version="3.10.2" />
<PackageReference Include="CG.Infrastructure.Authorization" />
paket add CG.Infrastructure.Authorization --version 3.10.2
#r "nuget: CG.Infrastructure.Authorization, 3.10.2"
#:package CG.Infrastructure.Authorization@3.10.2
#addin nuget:?package=CG.Infrastructure.Authorization&version=3.10.2
#tool nuget:?package=CG.Infrastructure.Authorization&version=3.10.2
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 storageClientGrantTypes
: Client grant type associationsClientRedirectUris
: Client redirect URI storageClientScopes
: Client scope associationsApiResources
: API resource definitionsApiScopes
: API scope definitionsIdentityResources
: Identity resource definitions
Providers
ClientProvider
: Client data access and management- Client retrieval by ID
- Client creation
- CORS origin querying
- Comprehensive logging
ApiResourceProvider
: API resource managementApiScopeProvider
: API scope managementIdentityResourceProvider
: 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 managementTokenCleanupService
: Operational store cleanup operationsTokenCleanupHostService
: Background service for token cleanup
Stores
ClientStore
: IdentityServer4 client store implementationResourceStore
: IdentityServer4 resource store implementationOperationalStore
: 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
- Fork the repository
- Create a feature branch
- Make your changes
- Add or update tests
- Ensure all tests pass
- 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 | Versions 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. |
-
net9.0
- AutoMapper (>= 15.0.1)
- CG.Infrastructure.Configuration (>= 3.10.1)
- CG.Infrastructure.Core (>= 3.10.8)
- CG.Infrastructure.Data (>= 3.10.9)
- CG.Infrastructure.Entity (>= 3.10.2)
- CG.Infrastructure.Services (>= 3.10.6)
- Duende.IdentityServer (>= 7.3.1)
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.