使用 MongoDb API 连接到 DocumentDB

Connect to DocumentDB using MongoDb API

我有以下问题

当我尝试使用 MongoDb API:

连接到 DocumentDB 数据库时
    var cstring = "mongodb://textadmin:<MY VERY SECRET PASSWORD>@<MY SERVER>.documents.azure.com:10250/?ssl=true"
    var client = new MongoDB.Driver.MongoClient(cstring)
    var server = client.GetServer()
    server.Ping()

我有以下错误:

认证失败,因为对方关闭了传输流

有什么想法可以更改代码(或服务器设置)吗?

您需要将 EnabledSslProtocols 设置为 TLS12 才能使用 MongoDB API 连接到 DocumentDB。默认情况下,Mongo C# 驱动程序不使用 TLS1.2,导致 SSL 握手期间连接失败。

示例代码:

MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, 10250);
settings.UseSsl = true;
settings.SslSettings = new SslSettings();
settings.SslSettings.EnabledSslProtocols = SslProtocols.Tls12;

MongoIdentity identity = new MongoInternalIdentity(dbName, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);

settings.Credentials = new List<MongoCredential>()
{
    new MongoCredential("SCRAM-SHA-1", identity, evidence)
};

MongoClient client = new MongoClient(settings);

参考:https://azure.microsoft.com/en-us/documentation/articles/documentdb-mongodb-samples/