ASP.NET Core MVC Intermediate

Keeping secrets safe: user secrets and Key Vault

Connection strings, API keys and passwords must never be committed to source control. This lesson shows where secrets belong at each stage: user secrets on your machine, environment variables or Azure Key Vault in production, and managed identities so many secrets disappear entirely.

Where secrets live
  1. Managed identityBest: no secret to store at all
  2. Azure Key VaultProduction secrets with rotation and audit
  3. Environment variablesApp settings on the hosting platform
  4. User secretsYour development machine only
  5. appsettings.jsonNever secrets: it is committed

Why it matters

Anything committed to Git stays in its history, even after you delete it, and leaked keys are found by bots within minutes on public repositories. A leaked database password or payment key can cost far more than the app earns.

ASP.NET Core configuration lets the code read Configuration["Payments:ApiKey"] the same way everywhere, while the value comes from a different safe place in each environment.

Development: user secrets

User secrets are stored in your user profile, outside the project folder, so they cannot be committed. They are loaded automatically in the Development environment.

Shell
dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:Shop" "Server=.;Database=Shop;Trusted_Connection=True;TrustServerCertificate=True"
dotnet user-secrets set "Payments:ApiKey" "test_key_from_the_provider_dashboard"
dotnet user-secrets list

Production: environment variables or Key Vault

Hosting platforms let you set app settings that become environment variables (use __ for nesting, such as Payments__ApiKey). For many secrets, rotation and auditing, use Azure Key Vault and read it with a managed identity, so the app needs no password to reach the vault.

C#
// Program.cs
if (builder.Environment.IsProduction())
{
    builder.Configuration.AddAzureKeyVault(
        new Uri("https://myshop-vault.vault.azure.net/"),
        new DefaultAzureCredential());   // managed identity in Azure, your login locally
}

// A Key Vault secret named Payments--ApiKey becomes Payments:ApiKey
var apiKey = builder.Configuration["Payments:ApiKey"];

Checklist

  • Keep appsettings.json free of secrets; it is committed.
  • Add a secret-scanning step to CI and turn on GitHub secret scanning.
  • Prefer managed identities for Azure SQL, Storage and Key Vault: no secret at all.
  • If a secret leaks, rotate it immediately; deleting the commit is not enough.
  • Never log configuration values or show them on error pages.

Interview question

Where should you store a database connection string for an ASP.NET Core app?

Never in appsettings.json in source control. In development use user secrets. In production use the hosting platform's app settings (environment variables) or Azure Key Vault read through a managed identity. Better still, use a managed identity for the database connection itself so the connection string contains no password.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.