Hardcoding secrets in application code or configuration files is one of the most common security mistakes in software development. Connection strings, API keys, certificates, and passwords are sensitive assets that should never be stored in source control or exposed in deployment artifacts.
Azure Key Vault provides a centralized and secure solution for managing secrets, encryption keys, and certificates. Combined with ASP.NET Core's configuration system and Managed Identity, it allows applications to access sensitive information without embedding credentials in code.
In this article, you'll learn how Azure Key Vault works, how to integrate it with .NET applications, and the best practices for secure secrets management.
Why Secrets Management Matters
Modern applications rely on numerous sensitive values, including:
Database connection strings
Third-party API keys
OAuth client secrets
Storage account keys
SSL/TLS certificates
Encryption keys
Storing these values in appsettings.json or source code increases the risk of accidental exposure, especially when repositories are shared across teams.
A centralized secrets management solution improves both security and maintainability.
What Is Azure Key Vault?
Azure Key Vault is a managed Azure service that securely stores and manages:
Secrets
Cryptographic keys
Digital certificates
Instead of retrieving sensitive values from configuration files, applications request them securely from Key Vault at runtime.
This approach ensures that secrets remain separate from application code and deployment pipelines.
How Azure Key Vault Works
A typical architecture looks like this:
ASP.NET Core Application
│
Managed Identity
│
Azure Key Vault
│
Secrets / Keys / Certificates
The application authenticates using Azure Managed Identity and retrieves secrets without requiring embedded credentials.
Creating a Key Vault
After creating an Azure Key Vault, you can add secrets through the Azure portal or Azure CLI.
Example:
az keyvault secret set \
--vault-name MyKeyVault \
--name "DatabaseConnection" \
--value "<connection-string>"
The secret is encrypted and stored securely within the vault.
Integrating Azure Key Vault with ASP.NET Core
Install the required NuGet package:
dotnet add package Azure.Extensions.AspNetCore.Configuration.Secrets
Configure Key Vault during application startup.
using Azure.Identity;
var builder = WebApplication.CreateBuilder(args);
var keyVaultUri = new Uri("https://mykeyvault.vault.azure.net/");
builder.Configuration.AddAzureKeyVault(
keyVaultUri,
new DefaultAzureCredential());
var app = builder.Build();
app.Run();
DefaultAzureCredential automatically selects the appropriate authentication mechanism based on the environment, making the same code work for local development and Azure deployments.
Reading Secrets
Once configured, secrets become part of the application's configuration system.
var connectionString =
builder.Configuration["DatabaseConnection"];
The application reads the secret exactly as it would from appsettings.json, without exposing the actual value in the project.
Using Managed Identity
One of the biggest advantages of Azure Key Vault is integration with Managed Identity.
Instead of storing Azure credentials inside the application, Azure automatically provides an identity for the deployed service.
Benefits include:
No stored passwords
Automatic credential rotation
Simplified authentication
Reduced security risks
Managed Identity is supported by services such as:
Azure App Service
Azure Container Apps
Azure Functions
Azure Kubernetes Service
Virtual Machines
Join the conversation! Your thoughts help the community grow.