Every Thursday I post SC-500 practice questions. Every Saturday I take one exam topic and go deep: the real problem, how it works, the fix, and the exam traps. Week 2: managed identities, and why "no secret at all" beats "a secret in a well-locked vault".
Lab story, lab names only (contoso.com). Screens are drawings, numbers are invented.
The problem: one connection string, three copies, zero owners
In this lab story, Contoso runs an order app, app-lab-orders, on Azure App Service. It talks to two things:
- the Azure SQL database
sqldb-lab-orders, with the SQL loginapp_ordersand its password - the storage account
stlaborders01, with the account key
Both live in the app's application settings as plain text. During a review, the team found the same SQL password in three places:
- the App Service configuration, readable by everyone with Contributor on the resource group (12 people)
- a
appsettings.Development.jsonfile in the Git repository, "only for local testing" - a pipeline variable that was never marked as secret
And the storage account key gives full access to every container, not just the one the app needs. Nobody had rotated either secret in 14 months, because nobody knew what would break.
Moving these secrets into Key Vault would help (deep dive #1 was about that). But the vault still holds a password that someone, someday, copies. The better question is: why does the app need a password at all?
How it works: the platform proves who the app is
A managed identity is an identity in Microsoft Entra ID that Azure creates and manages for a resource. The app asks the platform for a token, and the platform hands it one. There is no password, no certificate, nothing for the team to store or rotate.
| System-assigned | User-assigned | |
|---|---|---|
| Created | Turned on for one resource | Created as its own Azure resource |
| Lifecycle | Deleted when the resource is deleted | Lives until you delete it |
| Shared? | No, one resource only | Yes, can be attached to many resources |
| Good for | One app, one identity | Many instances that need the same access, or access ready before the resource exists |
The flow is always the same:
- The app asks the local token endpoint for a token for a target, for example Azure SQL or Azure Storage. On App Service it is the identity endpoint; on a VM it is the Instance Metadata Service (IMDS).
- Microsoft Entra ID issues a token for the managed identity.
- The app sends the token to the target. The target checks the identity's role or database user.
The important part is step 3. A managed identity on its own can do nothing. It is a name badge, not a key. Access comes only from the roles you give it on each target.
The fix: identity on, access granted, secrets gone
1. Find the secrets
List the app settings and connection strings, and look for passwords and keys:
az webapp config appsettings list -g rg-lab-orders -n app-lab-orders --query "[].name" -o tsv az webapp config connection-string list -g rg-lab-orders -n app-lab-orders --query "[].name" -o tsv In this lab: SqlConnection (with a password) and StorageConnection (with the account key).
2. Turn on the managed identity
az webapp identity assign -g rg-lab-orders -n app-lab-orders This is a system-assigned identity: it is created with the app and deleted with it. If the app runs in several regions and every copy needs the same access, use a user-assigned identity instead.
3. Give it the smallest access, on each target
For Azure SQL, create a database user for the identity (the database must have a Microsoft Entra admin), then give it only the roles it needs:
CREATE USER [app-lab-orders] FROM EXTERNAL PROVIDER; ALTER ROLE db_datareader ADD MEMBER [app-lab-orders]; ALTER ROLE db_datawriter ADD MEMBER [app-lab-orders]; For storage, a data role on the one container the app uses, not on the whole account:
SCOPE=$(az storage account show -n stlaborders01 --query id -o tsv)/blobServices/default/containers/invoices az role assignment create --role "Storage Blob Data Contributor" \ --assignee "$(az webapp identity show -g rg-lab-orders -n app-lab-orders --query principalId -o tsv)" \ --scope "$SCOPE" 4. Change the app to use tokens
The SQL connection string loses its password:
Server=tcp:sql-lab-01.database.windows.net;Database=sqldb-lab-orders;Authentication=Active Directory Default; For storage, the Azure SDK's DefaultAzureCredential picks up the managed identity on Azure and the developer's own sign-in on a laptop. Same code in both places, no key in either.
If one secret really must stay (for example, an API key from a payment partner), keep it in Key Vault and point to it with a Key Vault reference in the app settings, read with the same managed identity:
@Microsoft.KeyVault(SecretUri=https://kv-lab-orders.vault.azure.net/secrets/PartnerApiKey/) 5. Close the old doors
Removing the secret from the app is not enough. The old password and key still work for anyone who copied them:
- Drop the SQL login
app_orders, or turn on Microsoft Entra-only authentication on the server (SC-500 practice Q014). - Rotate both storage account keys, then set AllowSharedKeyAccess to
falseso no key works at all. - Delete the
appsettings.Development.jsonsecret from the repository history, and the pipeline variable.
6. Keep it that way
- Azure Policy has built-in definitions to audit App Service apps that do not use a managed identity, and to audit storage accounts that allow Shared Key access. Start in Audit.
- In Defender for Cloud, follow the matching recommendations.
SC-500 exam traps
- "Which identity type is deleted with the VM?" System-assigned. A user-assigned identity survives, and keeps its role assignments.
- "The app has a managed identity but gets 403." The identity has no role on the target. Turning the identity on grants nothing.
- "You removed a role, but the app still has access for a while." Managed identity tokens are cached by the platform for around 24 hours per target. Role and group changes can take that long to bite. Plan for it in an incident.
- "Assign an existing user-assigned identity to a VM. Which role do you need on the identity?" Managed Identity Operator (plus rights on the VM). Managed Identity Contributor is for creating and deleting identities.
- "Can a managed identity sign in to another tenant?" No, managed identities do not support cross-directory scenarios. Code that runs outside Azure (on-premises, another cloud, GitHub) uses workload identity federation or Azure Arc instead (SC-500 practice Q021).
Before and after in this lab story
| Before | After | |
|---|---|---|
| Secrets in app settings | 2 (SQL password, storage key) | 0 |
| Copies of the SQL password | 3 | 0, the login is dropped |
| Access to storage | Full account, by key | One container, by role |
| Rotation work per year | "Nobody dares" | None needed |
Coming next
Next deep dive: private endpoints vs service endpoints, and why "private" does not always mean what you think. On Thursday: SC-500 practice questions, as usual.
Sources
- What are managed identities for Azure resources?
- Managed identity best practice recommendations
- Managed identities FAQ
- Use managed identities for App Service
- Connect App Service to Azure SQL with a managed identity
- Use Key Vault references in App Service
- Manage user-assigned managed identities
Thanks for reading! I'm Cuong, and on CD Security Lab I rebuild real Microsoft security cases in a safe lab and explain them simply. Follow along:
- ▶️ YouTube (daily Shorts + explainers): CD Security Lab
- 💼 LinkedIn: Cuong Do
- 📘 Facebook: CD Security Lab
- 📸 Instagram: @cdsecuritylab
- 📝 More posts like this: Dev.to
Got a question about your own tenant? Drop it in the comments.





![Perception and Sensor Fusion — Chapter 05: IMU and Inertial Perception [KR]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fx5cdjbwd8758flk932no.png)
