Connecting to DocumentDB using MongoDb API

Category: azure documentdb


Fire Lizard on Sun, 25 Sep 2016 12:35:02

I have following problem

While I am trying to connect to the DocumentDB database using MongoDb API:

var cstring = "mongodb://textadmin:<MY VERY SECRET PASSWORD>@<MY SERVER>"       

var client = new MongoDB.Driver.MongoClient(cstring)       

var server = client.GetServer()       


I have the following error:

Authentication failed because the remote party has closed the transport stream

Any ideas what to change in the code (or in the server settings perhaps)?


sivethe on Mon, 03 Oct 2016 16:30:23

You need to set EnabledSslProtocols to TLS12 to be able to connect to DocumentDB using MongoDB API. By default, the Mongo C# driver does not use TLS1.2 causing the connection to fail during SSL handshake.

Sample Code:

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);