Noctusoft.EzLocalKeyVault.Configuration 1.0.0

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

EzLocalKeyVault

A lightweight, flexible key vault solution for .NET applications that provides seamless integration with Azure Key Vault for production environments and local key vault files for development.

Features

  • Hybrid Key Vault Integration: Automatically use Azure Key Vault in production and local key vault in development
  • Typed Configuration: Strongly-typed configuration classes with validation support
  • Direct Injection: Register configuration as concrete types or via the IOptions<T> pattern
  • Validation Support: Validate configuration using data annotations with strict or lenient modes
  • Fallback Behavior: Automatic fallback between Azure Key Vault and local key vault

Quick Start

1. Install the Package

# For local key vault only
dotnet add package Noctusoft.EzLocalKeyVault

# For hybrid Azure/Local key vault
dotnet add package Noctusoft.EzAzureLocalKeyVault

2. Create a Local Key Vault File

Create a .local-vault.json file in your project root:

{
  "API_KEY": "your-api-key",
  "CONNECTION_STRING": "Server=localhost;Database=YourDb;User Id=dev_user;Password=dev_password;"
}

3. Configure Your Application

Option 1: Local Key Vault Only
// Program.cs
builder.Configuration.AddEzLocalKeyVault();
Option 2: Hybrid Azure/Local Key Vault
// Program.cs
builder.Configuration.AddEzAzureLocalKeyVault(options => {
    options.AzureKeyVaultUri = "https://your-vault.vault.azure.net/";
    
    // This setting is optional - if not specified, it will use Azure Key Vault
    // If the local key vault file doesn't exist, it will automatically fall back to Azure Key Vault
    options.UseLocalKeyVault = builder.Environment.IsDevelopment();
    
    // You can also specify which environments should use local key vault
    // options.LocalKeyVaultEnvironments = new[] { "Development", "Staging" };
});

4. Use Typed Configuration

Define a configuration class:

using System.ComponentModel.DataAnnotations;
using Noctusoft.EzLocalKeyVault.Configuration;
using Noctusoft.EzLocalKeyVault.Configuration.Validation;

// Method 1: Property-level validation
// Only specific properties are required
public class AppSettings : IKeyVaultConfiguration
{
    // [ConfigurationRequired] will validate during configuration binding
    // Null or zero-length strings will raise errors
    [ConfigurationRequired(ErrorMessage = "API Key is required and cannot be empty")]
    public string ApiKey { get; set; } = string.Empty;
    
    // No annotation means this field won't be validated
    public string ConnectionString { get; set; } = string.Empty;
    
    // Standard validation attributes also work
    [Range(1, 3600, ErrorMessage = "Timeout must be between 1 and 3600 seconds")]
    public int Timeout { get; set; } = 30;
}

// Method 2: Class-level validation
// All properties in the class must have values
[ConfigurationRequired]
public class CriticalSettings : IKeyVaultConfiguration
{
    // All string properties must be non-null and non-empty
    public string ApiKey { get; set; } = string.Empty;
    public string ServiceUrl { get; set; } = string.Empty;
    
    // Nullable properties are optional (won't be validated)
    public string? OptionalSetting { get; set; }
    
    // But nullable properties WITH [ConfigurationRequired] WILL be validated
    [ConfigurationRequired]
    public string? RequiredNullableSetting { get; set; }
    
    // Value types are always considered valid
    public int Timeout { get; set; } = 30;
    
    // Reference types must be non-null
    public DatabaseSettings Database { get; set; } = new();
}

// Method 3: Class-level validation with nullable properties
// All non-nullable properties in the class must have values
[ConfigurationRequired]
public class CriticalSettingsWithNullable : IKeyVaultConfiguration
{
    // All string properties must be non-null and non-empty
    public string ApiKey { get; set; } = string.Empty;
    public string ServiceUrl { get; set; } = string.Empty;
    
    // Nullable properties are optional (won't be validated)
    public string? OptionalSetting { get; set; }
    
    // But nullable properties WITH [ConfigurationRequired] WILL be validated
    [ConfigurationRequired]
    public string? RequiredNullableSetting { get; set; }
    
    // Value types are always considered valid
    public int Timeout { get; set; } = 30;
    
    // Reference types must be non-null
    public DatabaseSettings? Database { get; set; }
}

Register and use the typed configuration:

// Option 1: Register without explicit validation
// Will automatically validate any [ConfigurationRequired] attributes
// For class-level validation, all errors will be aggregated into a single exception
builder.Services.AddTypedConfiguration<AppSettings>(builder.Configuration, "AppSettings");

// Option 2: Register with direct injection (also validates [ConfigurationRequired] attributes)
// Allows injecting AppSettings directly without IOptions wrapper
builder.Services.AddKeyVaultConfiguration<AppSettings>(builder.Configuration, "AppSettings");

// Option 3: Register with strict validation for all validation attributes
// Will throw ValidationException immediately after key vault evaluation if validation fails
// Use this in production to fail fast if configuration is invalid
builder.Services.AddValidatedConfiguration<AppSettings>(builder.Configuration, "AppSettings", strict: true);

// Option 4: Register with lenient validation for all validation attributes
// Will log validation errors but won't throw exceptions
// Useful for development environments
builder.Services.AddValidatedConfiguration<AppSettings>(builder.Configuration, "AppSettings", strict: false);

// Inject in your services
public class MyService
{
    private readonly AppSettings _settings;
    
    // Option 1: Inject via IOptions pattern
    public MyService(IOptions<AppSettings> options)
    {
        _settings = options.Value;
    }
    
    // Option 2: Inject directly (requires AddKeyVaultConfiguration)
    public MyService(AppSettings settings)
    {
        _settings = settings;
    }
}

5. Use in Configuration Files

In your appsettings.json:

{
  "AppSettings": {
    "ApiKey": "$(API_KEY)",
    "ConnectionString": "$(CONNECTION_STRING)",
    "Timeout": "$(TIMEOUT)"
  }
}

Documentation

Sample Applications

Security Notice

This library is designed for development and testing scenarios. When using in production:

  1. Never commit .local-vault.json to source control
  2. Use Azure Key Vault for production secrets
  3. Follow security best practices for managing Azure Key Vault access

License

MIT

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 was computed.  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

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 170 5/1/2025