Overview
The Azure Key Vault module provides seamless integration with Azure Key Vault for .NET applications, enabling secure storage and access to sensitive information such as passwords, API keys, connection strings, and certificates. This module extends the .NETIConfiguration service, making secrets accessible through the same configuration system you already use.
Installation
How It Works
The module integrates Azure Key Vault as a configuration source, allowing you to:- Store secrets securely in Azure Key Vault
- Access them through
IConfigurationin your application - Automatically refresh secrets
- Use different authentication methods
Architecture
Configuration
Basic Configuration
Add Key Vault settings toappsettings.json:
Configuration Parameters
| Parameter | Required | Description |
|---|---|---|
| Enabled | Yes | Enable/disable Key Vault integration |
| Endpoint | Yes | Your Key Vault URL |
| TenantId | Conditional | Azure AD tenant ID |
| ClientId | Conditional | Service principal client ID |
| Secret | Conditional | Service principal secret |
Authentication Methods
The module supports multiple authentication methods:1. Full Service Principal (Recommended for Production)
Provide all three: TenantId, ClientId, and Secret:2. ClientId Only (Managed Identity)
For applications already configured with managed identity:3. Credential Discovery (Local Development)
No credentials specified - usesDefaultAzureCredential:
DefaultAzureCredential tries authentication in this order:
- Environment variables
- Managed Identity
- Visual Studio credentials
- Azure CLI credentials
- Azure PowerShell credentials
Usage
Accessing Secrets
Secrets are accessed throughIConfiguration just like any other setting:
Strongly-Typed Configuration
Secret Naming
In Key Vault
Key Vault secret names must:- Use alphanumeric characters and hyphens only
- Be 1-127 characters long
- Cannot contain colons or underscores
Database--ConnectionString or ThirdPartyApi--ApiKey
In Application
The module automatically converts secret names:- Key Vault:
Database--ConnectionString - Configuration:
Database:ConnectionString
Common Scenarios
Connection Strings
Store in Key Vault:- Secret Name:
ConnectionStrings--DefaultConnection - Secret Value:
Server=myserver;Database=mydb;User=sa;Password=P@ssw0rd
API Keys
Store in Key Vault:- Secret Name:
ExternalApis--Stripe--SecretKey - Secret Value:
sk_live_...
Multi-Environment Secrets
Use different Key Vaults per environment: appsettings.Development.json:Setting Up Azure Key Vault
Create Key Vault
Azure Portal:- Navigate to Create a resource → Key Vault
- Configure:
- Name: Globally unique name
- Region: Same as your app
- Pricing tier: Standard or Premium
- Click Review + create
Add Secrets
Azure Portal:- Open your Key Vault
- Navigate to Secrets
- Click + Generate/Import
- Enter secret name and value
Grant Access
For Service Principal:Local Development
Option 1: Azure CLI Authentication
- Install Azure CLI
- Sign in:
- Set subscription:
- Use credential discovery in
appsettings.Development.json:
Option 2: User Secrets (Development Only)
For local development, use .NET user secrets instead of Key Vault: appsettings.Development.json:Advanced Features
Secret Versioning
Key Vault maintains version history:Automatic Refresh
Secrets are cached and automatically refreshed:Prefix Filtering
Load only secrets with specific prefix:Best Practices
Use Managed Identities
Use Managed Identities
Always use managed identities in Azure instead of storing service principal credentials:
Principle of Least Privilege
Principle of Least Privilege
Grant only the minimum required permissions:
- Development: Get, List
- Production: Get only
- CI/CD: Get, Set, Delete (for deployment automation)
Secret Rotation
Secret Rotation
Implement secret rotation strategy:
- Set expiration dates on secrets
- Monitor expiring secrets
- Use automated rotation where possible
- Test secret rotation process
Environment Separation
Environment Separation
Use separate Key Vaults for each environment:
- Development:
myapp-dev-kv - Staging:
myapp-staging-kv - Production:
myapp-prod-kv
Audit and Monitoring
Audit and Monitoring
Enable diagnostic logging:
- Track secret access
- Monitor failed authentication attempts
- Set up alerts for unusual activity
- Review audit logs regularly
Certificates
Key Vault can also store certificates:Troubleshooting
Common Issues
Issue: Access Denied- Verify credentials are correct
- Ensure
DefaultAzureCredentialcan find credentials - Check network connectivity to Azure
Enable Diagnostic Logging
Resources
Key Vault Documentation
Official Microsoft documentation
Best Practices
Security and operational guidance
Managed Identities
Using managed identities
Secret Rotation
Implementing secret rotation
