add user in cosmosdb Azure DocumentDB (with MongoDB compatibility) with entra authentication

Gajjala, Vinod Reddy 0 Reputation points
2026-03-11T07:06:42.62+00:00

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

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.

0 comments No comments

3 answers

Sort by: Oldest
  1. SUNOJ KUMAR YELURU 18,741 Reputation points MVP Volunteer Moderator
    2026-03-11T18:22:17.0666667+00:00

    Hello @Gajjala, Vinod Reddy,

    To add a user in Azure Cosmos DB (with MongoDB compatibility) and enable Microsoft Entra ID authentication, follow these steps:

    1. 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
    2. 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 readWrite or readAnyDatabase. Administrative users have full privileges, while non-administrative users can be granted either read-write or read-only permissions. 2
    3. 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/users with the naming format <cluster-name>/users/<principal-id>. 3
    4. Assign Roles: For non-administrative users, you can assign roles like readWriteAnyDatabase for full read-write access or readAnyDatabase for read-only access. Note that the readWriteAnyDatabase and clusterAdmin roles must be assigned together for full read-write access. 4
    5. 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+srv scheme and specify the authentication mechanism as MONGODB-OIDC

    Connect to Azure DocumentDB using role-based access control and Microsoft Entra ID


    If this answers your query, do click Accept Answer and Up-Vote for the same. And, if you have any further query do let us know.

    Was this answer helpful?


  2. Manoj Kumar Boyini 19,595 Reputation points Moderator
    2026-03-13T13:01:09.3866667+00:00

    Hi Gajjala, Vinod Reddy

    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 \
        

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

    Was this answer helpful?


  3. Avijit Gupta 0 Reputation points Microsoft Employee
    2026-11-23T05:05:00.6133333+00:00

    Hi @Gajjala, Vinod Reddy

    Thank you for raising this question and sharing your observations.

    Azure DocumentDB supports Microsoft Entra ID authentication, including the ability to configure secondary native users with read-only and read/write permissions.

    However, it is important to distinguish between authentication, which establishes the user's identity, and authorization, which determines the database operations that identity can perform.

    For configuring access:

    1. Enable** Microsoft Entra ID **authentication on your Azure DocumentDB cluster.
    2. Register** the Microsoft Entra ID **identity on the cluster.
    3. Configure** the appropriate **permissions using the supported role-management mechanism.

    For secondary users, Azure DocumentDB supports the following native roles:

    • readAnyDatabase: Read-only access.
    • readWriteAnyDatabase: Read/write access, assigned on the admin database.

    Please note that ARM** REST APIs, ARM-based Azure CLI commands, Bicep, and Terraform do not support secondary-user role **assignments. These roles should be managed through MongoDB-compatible drivers or tools such as mongosh.

    The following documentation provides guidance on configuring secondary users and their privileges:

    For Microsoft Entra ID identities specifically, please refer to the section covering secondary Microsoft Entra ID users in the second article.

    We appreciate the feedback and recognize the importance of more granular authorization for enterprise applications.

    Please let us know if you encounter any issues following the documented configuration steps.

    Regards,
    Avijit

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.