Introduction
Modern applications rely on sensitive information such as database connection strings, API keys, encryption certificates, and authentication secrets. Storing these values directly in source code or configuration files creates significant security risks and makes secret management difficult.
Azure Key Vault is a cloud service that securely stores and manages secrets, cryptographic keys, and certificates. It integrates seamlessly with ASP.NET Core and other Azure services, helping developers protect sensitive information while simplifying secret rotation and access control.
In this article, you'll learn how to use Azure Key Vault in ASP.NET Core, understand its core capabilities, and follow best practices for securing application secrets.
What Is Azure Key Vault?
Azure Key Vault is a managed service that securely stores and controls access to sensitive application data.
It supports three primary types of assets:
Secrets
Cryptographic keys
Certificates
Applications can retrieve these assets securely at runtime without storing them in source code.
Why Use Azure Key Vault?
Hardcoding secrets or storing them in configuration files can expose your application to unnecessary risk.
Azure Key Vault offers several advantages:
Centralized secret management
Secure storage with encryption
Automatic secret rotation support
Fine-grained access control
Audit logging
Integration with Azure services
Improved compliance
These capabilities help protect sensitive information throughout the application lifecycle.
What Can You Store?
Azure Key Vault can securely store various types of sensitive data.
Examples include:
Database connection strings
API keys
JWT signing secrets
Storage account keys
Third-party service credentials
TLS/SSL certificates
Encryption keys
Using a single secure location simplifies secret management across environments.
Install the Required Package
Install the Azure Key Vault configuration provider.
dotnet add package Azure.Extensions.AspNetCore.Configuration.Secrets
You also need the Azure Identity package.
dotnet add package Azure.Identity
These packages enable your application to authenticate and retrieve secrets from Key Vault.
Configure Azure Key Vault
Add Azure Key Vault during application startup.
using Azure.Identity;
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddAzureKeyVault(
new Uri("https://your-keyvault.vault.azure.net/"),
new DefaultAzureCredential());
The application can now access secrets stored in the configured Key Vault.
Store a Secret
Suppose you create a secret named:
DatabaseConnection
With a value such as:
Server=server;Database=AppDb;User Id=admin;Password=********;
The actual value remains securely stored in Azure Key Vault instead of your project files.
Read a Secret
Once configured, retrieve the secret like any other configuration value.
string connectionString =
builder.Configuration["DatabaseConnection"];
No additional API calls are required because the configuration provider handles secret retrieval.
Use Managed Identity
For Azure-hosted applications, use Managed Identity instead of storing credentials.
Benefits include:
No client secrets to manage
Automatic credential management
Improved security
Easier deployment
DefaultAzureCredential automatically uses the managed identity when the application runs in Azure.
Control Access with RBAC
Limit access to Key Vault using Azure Role-Based Access Control (RBAC).
Grant only the permissions required for each application or user.
Common roles include:
Key Vault Secrets User
Key Vault Secrets Officer
Key Vault Administrator
Following the principle of least privilege reduces the impact of compromised accounts.
Rotate Secrets Regularly
Secrets should not remain unchanged indefinitely.
Regular rotation helps:
Reduce the impact of credential exposure
Meet compliance requirements
Improve overall security
When rotating secrets:

Join the conversation! Your thoughts help the community grow.