Why does my ASP.NET Core app fail to start when using Azure Key Vault references in appsettings.json?

Asked 1 month ago Updated 11 days ago 248 views

0

Secrets Management Gone Wrong

Referencing Key Vault secrets directly in your configuration file looks elegant, but the runtime needs the Azure SDK to resolve those placeholders. If the SDK is missing or the app lacks the proper managed identity, startup throws a cryptic deserialization error.

Debugging the Startup Pipeline

First, confirm that the Azure.Extensions.AspNetCore.Configuration.Secrets package is installed and that your app is running with a system-assigned or user-assigned identity. Without that identity, the Key Vault client cannot authenticate, and the configuration provider silently returns empty values.

{
  "$schema": "https://json.schemastore.org/appsettings",
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  // This placeholder is resolved at runtime by the Azure Key Vault provider.
  "ConnectionStrings": {
    "DefaultConnection": "@Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/db-conn)"
  }
}
// Ensure the app has the Key Vault Secrets User role assigned in Azure RBAC.

Check the host builder logs for "KeyVaultConfigurationProvider" entries. If you see authentication failures, switch to a connection string with an explicit client secret temporarily, just to verify the network path is open.

1 Answer


0

The most common cause is that Azure App Service Key Vault references are not automatically resolved inside appsettings.json. The @Microsoft.KeyVault(...) syntax is meant for App Service configuration settings, such as application settings or connection strings. If you put that text directly in a JSON file, ASP.NET Core may read it as an ordinary string. Startup can then fail if your app expects the value to be a valid connection string, URI, or other setting.

If you want ASP.NET Core to load secrets directly from Key Vault, add the Key Vault configuration provider instead. For example, with the Azure.Extensions.AspNetCore.Configuration.Secrets and Azure.Identity packages:

using Azure.Identity;

// Read the vault URI from ordinary configuration, not from a secret reference.
var vaultUri = builder.Configuration["KeyVault:VaultUri"];

if (!string.IsNullOrWhiteSpace(vaultUri))
{
    // Add Key Vault after appsettings so its values can override them.
    builder.Configuration.AddAzureKeyVault(
        new Uri(vaultUri),
        new DefaultAzureCredential());
}

In Azure, the app's identity must be enabled and granted permission to read secrets—typically the Key Vault Secrets User role when using Azure RBAC. Locally, DefaultAzureCredential needs a usable developer login, such as an Azure CLI sign-in. A bad vault URI, missing permissions, firewall restrictions, or unavailable network access can also make the provider throw during startup because it fetches secrets as the application starts.

If you're using App Service references, put the reference in App Service configuration settings and check its resolution status in the portal. If you're using the .NET provider, configure the vault URI and credentials and let the provider retrieve secrets. The exact startup exception usually tells you whether the issue is an unresolved literal, authentication, authorization, or connectivity.

Write Your Answer