add user in cosmosdb Azure DocumentDB (with MongoDB compatibility) with entra authentication
i need to add user in cosmosdb Azure DocumentDB (with MongoDB compatibility) db...and the user is to connect to the via entra authentication only.... guide me how to add the user to connect to the db with privileges like read, read-write with entra authentiction...
2 answers
-
Manoj Kumar Boyini 17,060 Reputation points • Microsoft External Staff • Moderator
for Azure Cosmos DB (MongoDB API) you don’t use db.createUser()/mongosh to add Entra-authenticated users—everything is managed at the Azure level. Here’s a quick path you can follow:
Make sure Entra ID authentication is enabled
- In the Azure portal, go to your Cosmos DB for MongoDB vCore cluster → Settings → Authentication
- Under Authentication methods, select both Native DocumentDB and Microsoft Entra ID → Save
- Or use CLI:
az resource patch \
- Under Authentication methods, select both Native DocumentDB and Microsoft Entra ID → Save
--resource-group <rg>
--name <cluster>
--resource-type Microsoft.DocumentDB/mongoClusters
--properties '{"authConfig":{"allowedModes":["NativeAuth","MicrosoftEntraID"]}}'
--latest-include-preview ```Get the object ID of the Entra principal - For a user: ```dockerfile az ad user show --id ******@contoso.com --query objectId -o tsv ``` - For a service principal: ```dockerfile az ad sp show --id <appId> --query objectId -o tsv ``` - For a managed identity: ```dockerfile az identity show --resource-group <rg> --name <mi-name> --query principalId -o tsv ``` Register that principal on your cluster with the right roles - Read-only on database “mydb”: ```json az resource create \--resource-group <rg>
--name <cluster>/users/<object-id>
--resource-type Microsoft.DocumentDB/mongoClusters/users
--location <region>
--properties '{ "identityProvider":{"type":"MicrosoftEntraID","properties":{"principalType":"User"}}, "roles":[{"db":"mydb","role":"read"}] }'
--latest-include-preview ```- Read-write on “mydb”: ```json az resource create \--resource-group <rg>
--name <cluster>/users/<object-id>
--resource-type Microsoft.DocumentDB/mongoClusters/users
--location <region>
--properties '{ "identityProvider":{"type":"MicrosoftEntraID","properties":{"principalType":"User"}}, "roles":[{"db":"mydb","role":"readWrite"}] }'
--latest-include-preview ```You can also do this via the portal under Settings → Authentication → + Add Microsoft Entra ID. Connect using MONGODB-OIDC When you spin up mongosh or MongoDB Compass, use a connection string like: ```yaml mongodb+srv://<object-id>@<cluster>.mongo.cosmos.azure.com/?tls=true&authMechanism=MONGODB-OIDC &authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:https://ossrdbms-aad.database.windows.net ```
Replace `<object-id>` with the same principal ID you registered. Cosmos will exchange your Azure AD token for MongoDB access.I hope this helps. If you have any questions or concerns, please let us know—we're happy to assist further.
Reference docs:
- Configure Microsoft Entra ID authentication: https://learn.microsoft.com/azure/cosmos-db/mongodb/vcore/how-to-configure-entra-authentication
- Manage Entra ID users on the cluster: https://learn.microsoft.com/azure/cosmos-db/mongodb/vcore/how-to-configure-entra-authentication#manage-entra-id-users-on-the-cluster
- Connecting with OIDC (Compass/Mongosh): https://learn.microsoft.com/azure/documentdb/how-to-connect-role-based-access-control#connect-using-microsoft-entra-id-in-mongodb-compass-or-mongodb-shell
-
Manoj Kumar Boyini 17,060 Reputation points • Microsoft External Staff • Moderator
I hope you had a chance to review the information shared earlier, and I hope this information has been helpful! If you still have questions, please let us know what is needed in the comments so the question can be answered.
-
Saraswathi Devadula 16,025 Reputation points • Microsoft External Staff • Moderator
Hi Gajjala, Vinod Reddy
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet. In case if you have any resolution please do share that same with the community as it can be helpful to others. Otherwise, will respond with more details and we will try to help.
Sign in to comment - In the Azure portal, go to your Cosmos DB for MongoDB vCore cluster → Settings → Authentication
-
SUNOJ KUMAR YELURU 18,336 Reputation points • MVP • Volunteer Moderator
Hello @Gajjala, Vinod Reddy,
To add a user in Azure Cosmos DB (with MongoDB compatibility) and enable Microsoft Entra ID authentication, follow these steps:
- Enable Microsoft Entra ID Authentication: Ensure that Microsoft Entra ID authentication is enabled on your Azure Cosmos DB cluster. This can typically be done through the Azure portal by navigating to your Cosmos DB account and enabling the authentication method in the settings. After enabling, both NativeAuth and MicrosoftEntraID should be listed as enabled methods. 1
- Add Microsoft Entra ID Principals: You can add one or more Microsoft Entra ID principals (users, service principals, or managed identities) as administrator or non-administrator users. For non-administrative users, you can grant them specific roles such as
readWriteorreadAnyDatabase. Administrative users have full privileges, while non-administrative users can be granted either read-write or read-only permissions. 2 - Register the User: Register the Microsoft Entra ID principal on the cluster. This is done by creating an Azure resource of type
Microsoft.DocumentDB/mongoClusters/userswith the naming format<cluster-name>/users/<principal-id>. 3 - Assign Roles: For non-administrative users, you can assign roles like
readWriteAnyDatabasefor full read-write access orreadAnyDatabasefor read-only access. Note that thereadWriteAnyDatabaseandclusterAdminroles must be assigned together for full read-write access. 4 - Connect Using Microsoft Entra ID: Once the user is added and roles are assigned, they can connect to the Azure Cosmos DB cluster using a connection string that includes their Microsoft Entra ID credentials. The connection string format should include the
mongodb+srvscheme and specify the authentication mechanism asMONGODB-OIDC
Connect to Azure DocumentDB using role-based access control and Microsoft Entra ID
If this answers your query, do click
Accept Answerand Up-Vote for the same. And, if you have any further query do let us know.-
Gennadiy Dubina 0 Reputation points
but it does not work,
API accepts a list of roles with a single item only.
also it accepts db - admin, role - root.
when you try another you will get API error that value is not allowed
Example: "Provided database for role assignment is not valid (allowed: 'admin'...."
Sign in to comment
