Give an Azure app least-privilege access with a managed identity
Replace stored keys with a managed identity, then grant only the roles the app needs, on only the resources it uses.
An app on Azure usually talks to a few other services: a database, a storage account, maybe an AI service. The quick way to connect them is to copy a key or a connection string into the app's settings. It works, but now there is a secret to store, rotate and keep out of logs, and that one key usually opens everything in the account.
A managed identity removes the secret. Azure gives the app an identity in Microsoft Entra ID, the app gets a short-lived token from Entra ID through its Azure host when it needs one, and each service checks the token against the roles you gave that identity. Least privilege is the second half: give it only the roles it needs, only on the resources it uses.
The app's managed identity
Subscription No role here
Resource group No role here either
- Blob StorageStorage Blob Data ContributorKey access off
- Document IntelligenceCognitive Services User
- Microsoft FoundryFoundry User
- Cosmos DBCosmos DB Built-in Data ContributorA Cosmos data role
What you need
- An app that runs on Azure. The commands below use Azure Container Apps; App Service and Functions work the same way.
- The Azure CLI, signed in with rights to assign roles (Owner or User Access Administrator on the resources).
- The
Azure.Identitypackage in the .NET app.
1. Turn on the identity
A system-assigned identity lives and dies with the app, which is what you want for a single app.
az containerapp identity assign -n <app> -g <resource-group> --system-assigned
PRINCIPAL=$(az containerapp show -n <app> -g <resource-group> \
--query identity.principalId -o tsv)The app → Identity → System assigned → Status: On → Save.
2. Give it data roles, scoped to one resource each
Pick the smallest built-in role that does the job and scope it to the one resource, not the resource group or the subscription.
| Service | Role | What it allows |
|---|---|---|
| Blob Storage | Storage Blob Data Contributor | Read and write blobs |
| Document Intelligence | Cognitive Services User | Call the models (see the note below) |
| Microsoft Foundry | Foundry User | Call model deployments in a project |
| Cosmos DB | Cosmos DB Built-in Data Contributor | Read and write items (a Cosmos data role, see below) |
role() { # role name, resource id
az role assignment create \
--assignee-object-id "$PRINCIPAL" --assignee-principal-type ServicePrincipal \
--role "$1" --scope "$2"
}
role "Storage Blob Data Contributor" \
"$(az storage account show -n <storage> -g <resource-group> --query id -o tsv)"
role "Cognitive Services User" \
"$(az cognitiveservices account show -n <doc-intel> -g <resource-group> --query id -o tsv)"On each resource: Access control (IAM) → Add → Add role assignment → the role (under Job function roles) → Members: Managed identity → your app → Review + assign.
Cosmos DB has its own data-plane roles, assigned with a different command. That one has its own note.
For Document Intelligence, why Cognitive Services User and not the README's Data Reader is in its own note.
There is no Contributor or Owner in that list. The app can read and write data but can't create, change or delete the resources. When it needs a new blob container or Cosmos container, the deploy script creates it with your own rights.
3. Turn the keys off
While keys still work, anyone who finds one can skip your roles. Once the app runs on its identity, switch keys off.
# Storage: no shared keys, no public blobs
az storage account update -n <storage> -g <resource-group> \
--allow-shared-key-access false --allow-blob-public-access falseStorage account → Settings → Configuration: Allow storage account key access and Allow Blob anonymous access both Disabled → Save.
4. Use DefaultAzureCredential in the code
The same code works on your laptop (it uses your az login) and in Azure (it uses the managed identity).
using Azure.Identity;
using Azure.Storage.Blobs;
var credential = new DefaultAzureCredential();
var blobs = new BlobServiceClient(new Uri(builder.Configuration["Storage:BlobServiceUri"]!), credential);The settings now hold only endpoints, which are not secrets.
In production you can tell DefaultAzureCredential to try only the managed identity, so it never falls back to something else by accident. Azure.Identity 1.15.0 and later read an environment variable for this:
az containerapp update -n <app> -g <resource-group> \
--set-env-vars AZURE_TOKEN_CREDENTIALS=ManagedIdentityCredentialThe app → Revisions and replicas → Create new revision → your container → Environment variables → Add: name AZURE_TOKEN_CREDENTIALS, source manual, value ManagedIdentityCredential. Save, then create the revision.
Check that it works
- Call each service from the deployed app. A missing role shows up as a 403 from that service, not as a sign-in error.
- Role assignments can take a few minutes to apply. Wait before you debug.
- Try the old key (if you kept a copy): it should now be rejected.
Things I'd watch for
- "It works with Contributor on the resource group" is easy to leave in place. Scope it to the resource.
- Check for an assignment before creating it, so the deploy script can run again safely.
- Give each app its own identity. If apps share one, each gets the others' rights.
Sources
- What are managed identities? (Microsoft Learn)
- Managed identities in Container Apps (Microsoft Learn)
- Azure built-in roles (Microsoft Learn)
- Foundry roles: Foundry User (Microsoft Learn)
- Assign Azure roles in the portal (Microsoft Learn)
- Prevent Shared Key authorization (Microsoft Learn)
- Prevent anonymous blob access (Microsoft Learn)
- Credential chains and AZURE_TOKEN_CREDENTIALS (Microsoft Learn)