Secrets are the worst.
I hate managing them. You put them in Key Vault, but then you need a secret to access Key Vault. You put them in environment variables, but then they leak into logs. You rotate them, and everything breaks.
The best way to manage a password is to not have one.
Azure Managed Identity
For our MyDashboard backend, we decided to ban connection strings containing passwords. Instead, we use Azure System-Assigned Managed Identity. The App Service itself becomes an identity in Azure AD, and we grant that identity permission to log into the PostgreSQL database.
The Setup
It requires three pieces, and it was a bit tricky to figure out the first time.
1. Infrastructure (Bicep)
You need to enable the identity on your App Service. This is the easy part.
|
|
2. Database User
You need to log into Postgres (as an admin) and create a user that maps to the Managed Identity.
|
|
(Pro tip: The password ‘123’ here is a dummy requirement by Postgres. It will never be used. Azure AD handles the actual check).
3. The Code (.NET)
This is where DefaultAzureCredential shines.
|
|
Now, if you check your appsettings.json, strictly speaking, there are no secrets. Just names. If an attacker steals your config file, they can’t access your database. Splendid!