When you begin programming in opposition to DocumentDB, the first actual step is to connect. So to connect to your DocumentDB account you'll want matters;
- Endpoint
- Authorization Key
Endpoint
Endpoint is the URL in your DocumentDB account and it's far built by combining your DocumentDB account call with .Documents.Azure.Com. Let’s visit the Dashboard.
Now, click on at the created DocumentDB account. You will see the information as proven inside the following photograph.
When you choose the ‘Keys’ choice, it'll show extra statistics as shown in the following photograph. You can even see the URL to your DocumentDB account, which you may use as your endpoint.
Authorization Key
Authorization key contains your credentials and there are forms of keys. The master key allows full get entry to to all resources within the account, while aid tokens permit restricted access to unique sources.
Master Keys
- There's nothing you cannot do with a grasp key. You can blow away your complete database in case you want, using the grasp key.
- For this cause, you certainly do not need to be sharing the master key or distributing it to purchaser environments. As an introduced security measure, it is an amazing concept to trade it frequently.
- There are virtually two master keys for every database account, the primary and the secondary as highlighted within the above screenshot.
Resource Tokens
- You also can use aid tokens instead of a master key.
- Connections primarily based on aid tokens can handiest get entry to the sources distinct by way of the tokens and no different sources.
- Resource tokens are based on person permissions, so first you create one or more users, and these are described at the database stage.
- You create one or extra permissions for every consumer, primarily based at the resources that you want to permit every consumer to get entry to.
- Each permission generates a aid token that permits either study-handiest or full get entry to to a given aid and that may be any person aid in the database.
Let’s go to console utility created in bankruptcy three.
Step 1 − Add the following references inside the Program.Cs document.
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
Step 2 − Now upload Endpoint URL and Authorization key. In this example we will be the usage of number one key as Authorization key.
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey =
"BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
Step 3 − Create a brand new example of the DocumentClient in asynchronous assignment known as CreateDocumentClient and instantiate new DocumentClient.
Step 4 − Call your asynchronous task out of your Main technique.
Following is the entire Program.Cs document thus far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
namespace DocumentDBDemo {
class Program {
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
static void Main(string[] args) {
try {
CreateDocumentClient().Wait();
} catch (Exception e) {
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
Console.ReadKey();
}
private static async Task CreateDocumentClient() {
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
}
}
}
In this bankruptcy, we've learnt how to connect to a DocumentDB account and create an example of the DocumentClient magnificence.