Share via

Steps to create a single user

Wilian Jose Lima 1 Reputation point
2021-06-30T15:25:28.553+00:00

Hi everyone
we need to know steps to create a user to connect in cosmos db.
How i create a user to access and read documents on cosmosdb ?

I don't want the user using connection string with user which is on the blade

Azure Cosmos DB
Azure Cosmos DB

An Azure NoSQL database service for app development.


1 answer

Sort by: Most helpful
  1. Anurag Sharma 17,636 Reputation points
    2021-07-01T08:07:30.963+00:00

    Hi @Wilian Jose Lima , welcome to Microsoft Q&A forum.

    You can refer to below documentation to create and assign necessary permissions to users:
    Secure access to data in Azure Cosmos DB

    Creating a User:

    //Create a user.  
    Database database = benchmark.client.GetDatabase("SalesDatabase");  
      
    User user = await database.CreateUserAsync("User 1");  
    

    Creating Permission:

    // Create a permission on a container and specific partition key value  
    Container container = client.GetContainer("SalesDatabase", "OrdersContainer");  
    user.CreatePermissionAsync(  
        new PermissionProperties(  
            id: "permissionUser1Orders",  
            permissionMode: PermissionMode.All,  
            container: container,  
            resourcePartitionKey: new PartitionKey("012345")));  
    

    Code sample to read permission for user:

    //Read a permission, create user client session.  
    PermissionProperties permissionProperties = await user.GetPermission("permissionUser1Orders")  
      
    CosmosClient client = new CosmosClient(accountEndpoint: "MyEndpoint", authKeyOrResourceToken: permissionProperties.Token);  
    

    You can find the rest api and the permissions in below link as well:

    https://learn.microsoft.com/en-us/rest/api/cosmos-db/users
    https://learn.microsoft.com/en-us/rest/api/cosmos-db/permissions

    Please let me know if this helps.

    ----------

    If answer helps, please mark it 'Accept Answer'

    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.