Use Cosmos DB from .NET with roles, not keys
Assign a Cosmos DB data role, connect with DefaultAzureCredential, then turn off key access.
A Cosmos DB account key is a secret you have to store, rotate and keep out of source control. A key or connection string committed to a repo is the most common way Cosmos DB credentials leak.
Cosmos DB has its own data-plane roles that work with Microsoft Entra ID. Give the app one of those, connect with a token, and turn the keys off.
Why it's different from other services
For most Azure services, data access is a normal Azure role (az role assignment create). Cosmos DB for NoSQL keeps its data roles separate:
- Control plane (Azure roles): create and delete accounts, databases and containers.
- Data plane (Cosmos roles): read and write items.
The built-in data roles are Cosmos DB Built-in Data Reader (00000000-0000-0000-0000-000000000001) and Cosmos DB Built-in Data Contributor (00000000-0000-0000-0000-000000000002). Creating databases and containers is a control-plane job, so the app's data role doesn't cover it. That split is useful: the app reads and writes items, and the deploy script creates containers. Even the Azure role Cosmos DB Operator, made for managing accounts, can't read the data or the keys.
| Action | Built-in Data ReaderCosmos data role | Built-in Data ContributorCosmos data role | Cosmos DB OperatorAzure role |
|---|---|---|---|
| Read items and run queries | Yes | Yes | No |
| Create, replace and delete items | No | Yes | No |
| Create databases and containers | No | No | Yes |
| Read or regenerate the account keys | No | No | No |
1. Assign the data role
PRINCIPAL=<managed-identity-principal-id>
az cosmosdb sql role assignment create \
--account-name <account> \
--resource-group <resource-group> \
--role-definition-id 00000000-0000-0000-0000-000000000002 \
--principal-id "$PRINCIPAL" \
--scope "/"The portal can't manage Cosmos DB data roles. Run this in Cloud Shell (>_ in the portal's top bar):
az cosmosdb sql role assignment create --account-name <account> --resource-group <resource-group> \
--role-definition-id 00000000-0000-0000-0000-000000000002 \
--principal-id <managed-identity-principal-id> --scope "/"--scope "/" covers the whole account. To limit it to one container, use /dbs/<database>/colls/<container>.
Do the same for your own account, so the code also works on your laptop. Use the object ID from az ad signed-in-user show --query id -o tsv.
2. Connect with DefaultAzureCredential
using System.Text.Json;
using Azure.Identity;
using Microsoft.Azure.Cosmos;
// One client per app, as the SDK recommends.
builder.Services.AddSingleton(_ => new CosmosClient(
builder.Configuration["Cosmos:Endpoint"], // https://<account>.documents.azure.com:443/
new DefaultAzureCredential(),
new CosmosClientOptions
{
UseSystemTextJsonSerializerWithOptions = new JsonSerializerOptions(JsonSerializerDefaults.Web),
}));The only setting is the endpoint, and it isn't a secret.
3. Create containers outside the app
When the app signs in with Entra ID, the SDK's create, replace and delete methods for databases and containers fail with a 403, because they are control-plane work. So don't call CreateContainerIfNotExistsAsync from the app. Create containers in your deploy script, which runs with your own rights:
az cosmosdb sql container create -a <account> -g <resource-group> \
-d <database> -n orders --partition-key-path /customerIdThe account → Data Explorer → New Container: Database id <database>, Container id orders, Partition key /customerId → OK.
4. Turn off key access
Once nothing uses a key, switch keys off for the whole account:
az resource update --resource-type "Microsoft.DocumentDB/databaseAccounts" \
-n <account> -g <resource-group> --set properties.disableLocalAuth=trueThe portal offers this only when you create an account: Security → Key-based authentication → Disable. For an existing account, run this in Cloud Shell:
az resource update --resource-type "Microsoft.DocumentDB/databaseAccounts" \
-n <account> -g <resource-group> --set properties.disableLocalAuth=trueFrom now on, every request has to come with an Entra token. Old connection strings stop working.
Check that it works
database.ReadAsync()reads only the database's metadata, which both built-in data roles allow. It proves the endpoint, the sign-in and the role in one call.- On a 403, list the assignments with
az cosmosdb sql role assignment listand check the principal ID and the scope. - Once keys are off, the Data Explorer in the Azure portal sends data requests with your Entra ID sign-in by default, so your user needs a data role as well.
Sources
- Cosmos DB data roles and turning keys off (Microsoft Learn)
- Cosmos DB data actions and built-in roles (Microsoft Learn)
- Create a container in Data Explorer (Microsoft Learn)
- Data Explorer with Microsoft Entra ID (Microsoft Learn)
- Troubleshoot 403 Forbidden in Cosmos DB (Microsoft Learn)
- Which Cosmos DB role does my app need? (Cosmos DB blog)