Serhat Ozgel
Serhat Ozgel

Reputation: 23766

Cannot connect to MongoDB 3.0.0 by using C# Driver 1.10

I installed a MongoDB 3.0.0 instance on an Azure server and created a user on admin db:

{ 
    "_id" : "admin.skMongo", 
    "user" : "skMongo", 
    "db" : "admin", 
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : NumberInt(10000), 
            "salt" : "(someSalt)", 
            "storedKey" : "(somekey)", 
            "serverKey" : "(somekey)"
        }
    }, 
    "customData" : {

    }, 
    "roles" : [
        {
            "role" : "dbOwner", 
            "db" : "(my database)"
        }, 
        {
            "role" : "readWrite", 
            "db" : "(my database)"
        }, 
        {
            "role" : "readWriteAnyDatabase", 
            "db" : "admin"
        }
    ]
}

By using MongoChef as a client, I can connect to this instance without running into problems. I am using these settings:

Server: (my ip address)
Port: 27017
Database: admin
Username: skMongo
Password: (mypassword)

But when I try to connect by using Offical C# MongoDB Driver 1.10.0.62, I get this error:

Unable to connect to server 23.97.171.16:27017: Invalid credential for database 'admin'..

on this line:

return collection.Find(q).ToList();

This is the code I am using to initialize mongo db server:

var credentials = MongoCredential.CreateMongoCRCredential("admin", "skMongo", "(my password)");

_server = new MongoServer(
    new MongoServerSettings
    {
        Server = string.IsNullOrEmpty(port) ? new MongoServerAddress(host) : new MongoServerAddress(host, int.Parse(port)),
        Credentials = new[] { credentials },
    });

_database = _server.GetDatabase("(my database)");
return _database;

This code was working on the previous version of mongo. What am I doing wrong?

Upvotes: 1

Views: 3030

Answers (1)

Serhat Ozgel
Serhat Ozgel

Reputation: 23766

I have solved this by changing this line:

var credentials = MongoCredential.CreateMongoCRCredential("admin", "skMongo", "(my password)");

to this:

var credentials = MongoCredential.CreateScramSha1Credential("admin", "skMongo", ("my password"));

Upvotes: 3

Related Questions