How would you protect secrets and rotate credentials for a .NET application deployed to Azure?
Swiggy · Full Stack Developer · 3-4-years · technical
Published
Year not specified · .NET, Azure, Authentication & Security
Community answers
Posted by wasAsked editorial collection
In Azure, prefer managed identity so the application can access Key Vault without storing a client secret. Grant the identity only the specific data-plane permissions it needs and use DefaultAzureCredential.
Example:
var client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential());
KeyVaultSecret secret = await client.GetSecretAsync("PaymentApiKey", cancellationToken: ct);
Use configuration providers and cache secrets for an appropriate period rather than calling the vault on every request. Separate vaults/identities by environment, enable audit logs, rotation, soft delete, and purge protection. Never commit secrets, place them in images, expose them to the browser, or print them in logs and exceptions.
Backbencher Developer · · 0 likes
The goal is to have no secrets in code or configuration files, and to make rotation routine rather than an emergency.
1. Remove secrets where possible: managed identity
- Give the App Service, Container App, Function or AKS workload a managed identity.
- Access Azure SQL, Storage, Service Bus, Key Vault and Cosmos DB with Microsoft Entra authentication via DefaultAzureCredential — no passwords or keys to store or rotate.
var client = new SecretClient(new Uri(vaultUri), new DefaultAzureCredential());
2. Keep remaining secrets in Azure Key Vault
- Third-party API keys, certificates and any connection strings that cannot use identity.
- Load them through the configuration provider (AddAzureKeyVault) or App Service Key Vault references (@Microsoft.KeyVault(SecretUri=...)).
- Grant least privilege with Azure RBAC (Key Vault Secrets User for the app only), enable soft delete and purge protection, and restrict network access (private endpoints or firewall).
3. Rotation
- Use versioned secrets: reference the secret without a version so the app picks up the latest, or reload periodically (ReloadInterval in the Key Vault configuration provider; Key Vault references refresh within about a day or on restart).
- For keys with primary and secondary values (Storage, Service Bus, Cosmos DB): switch the app to the secondary key, regenerate the primary, switch back — no downtime.
- Automate with Key Vault rotation policies or Event Grid "near expiry" events triggering an Azure Function.
- Certificates: let Key Vault auto-renew from an integrated CA, and alert before expiry.
4. Development and CI/CD
- Developers use dotnet user-secrets locally or their own Entra identity, never shared production secrets.
- Pipelines authenticate to Azure with workload identity federation (OIDC) instead of stored service principal secrets.
- Scan repositories for leaked secrets (GitHub secret scanning, gitleaks) and rotate immediately if one leaks.
5. Monitor
- Enable Key Vault diagnostic logs and alerts on unusual access, failed authentication or secret changes.
- Do not log configuration values or connection strings.