The recommended way is managed identity plus Azure RBAC, so the application never stores a credential to reach Key Vault. Step 1 — Enable a managed identity Turn on the system-assigned identity for the App Service or Function App (or create a user-assigned identity and attach it). Step 2 — Grant access Assign the Key Vault Secrets User role to that identity on the vault (or on a specific secret). In older vaults using access policies, grant Get and List permissions for secrets. Step 3 — Read secrets in code Option A: configuration provider (loads secrets at startup into IConfiguration) builder.Configuration.AddAzureKeyVault( new Uri(builder.Configuration["KeyVault:Uri"]!), new DefaultAzureCredential()); var conn = builder.Configuration.GetConnectionString("OrdersDb"); // secret named "ConnectionStrings--OrdersDb" maps to ConnectionStrings:OrdersDb Option B: SecretClient for on-demand access var client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential()); KeyVaultSecret secret = await client.GetSecretAsync("PaymentApiKey"); Option C: App Service and Functions Key Vault references — no code changes App setting value: @Microsoft.KeyVault(SecretUri=https://myvault.vault.azure.net/secrets/PaymentApiKey/) The platform resolves it using the app's managed identity, and the app reads it as a normal setting. How DefaultAzureCredential helps - In Azure it uses the managed identity. - On a developer machine it uses Visual Studio, Azure CLI or azd sign-in, so the same code works locally without secrets. Additional practices - Separate vaults per environment. - Network restrictions (private endpoint or firewall) and soft delete with purge protection. - Cache secrets and avoid calling Key Vault on every request (it has throttling limits). - Plan rotation: reference secrets without a version to get the latest, and reload configuration (ReloadInterval) or restart after rotation. - Even better, use managed identity directly to access SQL, Storage and Service Bus, so those connection secrets do not need to exist.
Ravindra Wadile · · 0 likes