使用MongoDb API连接到DocumentDB
本文关键字:DocumentDB 连接 API MongoDb 使用 | 更新日期: 2023-09-27 18:08:58
我有以下问题
当我试图连接到使用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/