Tuesday, July 02, 2013

c# mongoDB driver MongoCredentials replacement

As of version 1.8 of the official mongoDB c# driver the MongoCredentials-class is gone ...

Here's how to rewrite your code

var client = new MongoClient("mongodb://192.168.1.2");
var server = client.GetServer();
var db = server.GetDatabase("myDatabase", 
       new MongoCredentials("myUser", "myPassword"));
Becomes:
var cls = new MongoClientSettings()
{
    Credentials = new MongoCredential[] { MongoCredential.CreateMongoCRCredential("myDatabase", "myUser", "myPassword") },
    Server = new MongoServerAddress("192.168.1.2")
};

var client = new MongoClient(cls);
var server = client.GetServer();
db = server.GetDatabase("myDatabase");

No comments: