Storing sensitive settings such as database connection strings and third-party API credentials directly inside appsettings.json introduces significant security vulnerabilities. Azure Key Vault provides a centralized, secure cloud solution for managing application secrets.
Prerequisites and NuGet Packages
To integrate Azure Key Vault into your ASP.NET Core application, install the following packages:
- Azure.Identity: Provides seamless authentication via Microsoft Entra ID.
- Azure.Extensions.AspNetCore.Configuration.Secrets: Integrates Key Vault seamlessly into the ASP.NET Core configuration framework.
Configuring Key Vault in Program.cs
Utilize DefaultAzureCredential to support passwordless authentication across local development and production environments:
using Azure.Identity;
var builder = WebApplication.CreateBuilder(args);
if (!builder.Environment.IsDevelopment())
{
var keyVaultUri = builder.Configuration["AzureKeyVault:VaultUri"];
builder.Configuration.AddAzureKeyVault(
new Uri(keyVaultUri),
new DefaultAzureCredential()
);
}
var app = builder.Build();Accessing Secrets in Services
Once configured, secrets in Azure Key Vault automatically become part of standard ASP.NET Core IConfiguration injection.