user306080
user306080

Reputation: 1519

Connect to DocumentDB using MongoDb API

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>.documents.azure.com:10250/?ssl=true"
    var client = new MongoDB.Driver.MongoClient(cstring)
    var server = client.GetServer()
    server.Ping()

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

Upvotes: 0

Views: 884

Answers (1)

Siddhesh Vethe
Siddhesh Vethe

Reputation: 226

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

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

Upvotes: 1

Related Questions