private static async Task TestPermissions(IDocumentClient client, User user, string collectionLink) { Permission perm = client.CreatePermissionQuery(user.PermissionsLink).AsEnumerable().First(p => p.ResourceLink == collectionLink); string resourceToken = perm.Token; dynamic documentDefinition = new { name = "New Customer 1", address = new { addressType = "Main Office", addressLine1 = "123 Main Street", location = new { city = "Brooklyn", stateProvinceName = "New York" }, postalCode = "11229", countryRegionName = "United States" } }; Console.WriteLine($"Trying to create & delete document as user {user.Id}"); try { string endpoint = ConfigurationManager.AppSettings["CosmosDbEndpoint"]; using (DocumentClient restrictedClient = new DocumentClient(new Uri(endpoint), resourceToken)) { dynamic document = await restrictedClient.CreateDocumentAsync(collectionLink, documentDefinition); Console.WriteLine($"Successfully created document: {document.Resource.id}"); RequestOptions options = new RequestOptions { PartitionKey = new PartitionKey("11229") }; await restrictedClient.DeleteDocumentAsync(document.Resource._self, options); Console.WriteLine($"Successfully deleted document: {document.Resource.id}"); } } catch (Exception exception) { Console.WriteLine($"ERROR: {exception.Message}"); } }
// Permissions private static void ListPermissions(IDocumentClient client, User user) { Console.WriteLine($">>> View Permissions for {user.Id} <<<"); List <Permission> permissions = client.CreatePermissionQuery(user.PermissionsLink).ToList(); int i = 0; foreach (Permission permission in permissions) { i++; Console.WriteLine(); Console.WriteLine($"Permission #{i}"); PrintPermission(permission); } Console.WriteLine($"Total permissions for {user.Id}: {permissions.Count}"); }