Documentdb 简明教程
DocumentDB - Connect Account
在你开始对 DocumentDB 编程时,第一步是连接。因此,要连接到你的 DocumentDB 帐户,你需要两样东西;
When you start programming against DocumentDB, the very first step is to connect. So to connect to your DocumentDB account you will need two things;
-
Endpoint
-
Authorization Key
Endpoint
端点是你的 DocumentDB 帐户的 URL,它是通过将你的 DocumentDB 帐户名与 .documents.azure.com 结合来构建的。我们进入仪表板。
Endpoint is the URL to your DocumentDB account and it is constructed by combining your DocumentDB account name with .documents.azure.com. Let’s go to the Dashboard.

现在,单击创建的 DocumentDB 帐户。你将看到详细信息,如下所示。
Now, click on the created DocumentDB account. You will see the details as shown in the following image.

当你选择“密钥”选项时,它将显示更多信息,如下所示。你还会看到你的 DocumentDB 帐户的 URL,你可以用它作为你的端点。
When you select the ‘Keys’ option, it will display additional information as shown in the following image. You will also see the URL to your DocumentDB account, which you can use as your endpoint.

Authorization Key
授权密钥包含你的凭证,并且有两种类型的密钥。主密钥允许访问帐户内的所有资源,而资源令牌允许受限访问特定资源。
Authorization key contains your credentials and there are two types of keys. The master key allows full access to all resources within the account, while resource tokens permit restricted access to specific resources.
Master Keys
-
There’s nothing you can’t do with a master key. You can blow away your entire database if you want, using the master key.
-
For this reason, you definitely don’t want to be sharing the master key or distributing it to client environments. As an added security measure, it’s a good idea to change it frequently.
-
There are actually two master keys for each database account, the primary and the secondary as highlighted in the above screenshot.
Resource Tokens
-
You can also use resource tokens instead of a master key.
-
Connections based on resource tokens can only access the resources specified by the tokens and no other resources.
-
Resource tokens are based on user permissions, so first you create one or more users, and these are defined at the database level.
-
You create one or more permissions for each user, based on the resources that you want to allow each user to access.
-
Each permission generates a resource token that allows either read-only or full access to a given resource and that can be any user resource within the database.
让我们进入在第 3 章中创建的控制台应用程序。
Let’s go to console application created in chapter 3.
Step 1 − 在 Program.cs 文件中添加以下引用。
Step 1 − Add the following references in the Program.cs file.
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
Step 2 − 现在添加端点 URL 和授权密钥。在此示例中,我们将使用主密钥作为授权密钥。
Step 2 − Now add Endpoint URL and Authorization key. In this example we will be using primary key as Authorization key.
请注意,在你的情况下,端点 URL 和授权密钥都应有所不同。
Note that in your case both Endpoint URL and authorization key should be different.
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey =
"BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
Step 3 − 在称为 CreateDocumentClient 的异步任务中新建一个 DocumentClient 的实例,并实例化新的 DocumentClient。
Step 3 − Create a new instance of the DocumentClient in asynchronous task called CreateDocumentClient and instantiate new DocumentClient.
Step 4 − 从你的 Main 方法调用异步任务。
Step 4 − Call your asynchronous task from your Main method.
以下是迄今为止完整的 Program.cs 文件。
Following is the complete Program.cs file so 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);
}
}
}
在本章中,我们学习了如何连接到 DocumentDB 帐户和新建一个 DocumentClient 类的实例。
In this chapter, we have learnt how to connect to a DocumentDB account and create an instance of the DocumentClient class.