, Mark Joseph Colibao, home

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.

Written 3 min read

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.

Where the roles go

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.Identity package 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.

bash
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)

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.

ServiceRoleWhat it allows
Blob StorageStorage Blob Data ContributorRead and write blobs
Document IntelligenceCognitive Services UserCall the models (see the note below)
Microsoft FoundryFoundry UserCall model deployments in a project
Cosmos DBCosmos DB Built-in Data ContributorRead and write items (a Cosmos data role, see below)
bash
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)"

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.

bash
# 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 false

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).

C#
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:

bash
az containerapp update -n <app> -g <resource-group> \
  --set-env-vars AZURE_TOKEN_CREDENTIALS=ManagedIdentityCredential

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