3

This thing will make me go crazy now. I'm trying to get it to work for 3 hours without any result. I installed mongodb 2.8rc4. I created a user account in admin db. I can connect to mongod using mongo localhost/agp -u dato0011 -p "MyPassword". I can authenticate it via db.auth, but for an unknown reason, this thing doesn't work from C# driver.

Connection string is mongodb://dato0011:MyPWD@localhost/agp

I initialize the db with the following code:

        var client = new MongoClient(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
        var server = client.GetServer();
        var db = server.GetDatabase("agp");

but when I try save, I get an exception saying:

Unable to connect to server 192.168.0.100:27017: Invalid credential for database 'agp'..

and deep below inner exceptions, there's this message:

{ "authenticate" : 1, "user" : "dato0011", "nonce" : "fc65b3c269560533", "key" : "35589d31830b76c72358343bc054105f" }

and this

{"Command 'authenticate' failed: auth failed (response: { \"ok\" : 0.0, \"errmsg\" : \"auth failed\", \"code\" : 18 })"}

Anyone has any idea what am I doing wrong? Thanks.

UPDATE

Don't know if it will help, but here's the code that throws exception:

Users.Save(user);

where Users is

Users = db.GetCollection<User>("users", WriteConcern.W1);

UPDATE 2 Apparently the exception message is a bit misleading. The driver does connect to mongod, but it can't authenticate for an unknown reason. username and password parameters are passed correctly. Here's the user in admin db.

> db.system.users.find().toArray()
[
        {
                "_id" : "agp.dato0011",
                "user" : "dato0011",
                "db" : "agp",
                "credentials" : {
                        "SCRAM-SHA-1" : {
                                "iterationCount" : 10000,
                                "salt" : "GjlDJOiKf2Xn1VO/1MhWXA==",
                                "storedKey" : "vAi30QZLFkCZf6ISm5TIfIWPwZY=",
                                "serverKey" : "DBmbbRLrLXEIxFCuZ52VaSnRWwo="
                        }
                },
                "roles" : [
                        {
                                "role" : "readWrite",
                                "db" : "agp"
                        },
                        {
                                "role" : "dbAdmin",
                                "db" : "agp"
                        }
                ]
14
  • Where the port 27017 is set? Commented Jan 2, 2015 at 19:32
  • You connect to localhost from the command line, but to 192.168.0.100 from C# - oversight or on purpose? Commented Jan 2, 2015 at 19:33
  • @HamletHakobyan I tried already, no difference
    – Davita
    Commented Jan 2, 2015 at 19:34
  • 2
    @Davita it's not a connectivity issue.. it's an authentication one.
    – i3arnon
    Commented Jan 2, 2015 at 19:34
  • 1
    What version of the driver are you using? Authentication in server 2.8 has changed and the current stable version of the driver (1.9.2) does not support it. These easiest solution is to use the 1.10.0-rc1 driver or use a 2.6 server. Commented Jan 2, 2015 at 21:10

2 Answers 2

1

Use MongoCredentials on MongoClientSettings -

    string username = "user";
    string password = "password";
    string mongoDbAuthMechanism = "SCRAM-SHA-1";
    MongoInternalIdentity internalIdentity = 
              new MongoInternalIdentity("admin", username);
    PasswordEvidence passwordEvidence = new PasswordEvidence(password);
    MongoCredential mongoCredential = 
         new MongoCredential(mongoDbAuthMechanism, 
                 internalIdentity, passwordEvidence);
    List<MongoCredential> credentials = 
               new List<MongoCredential>() {mongoCredential};


    MongoClientSettings settings = new MongoClientSettings();
    // comment this line below if your mongo doesn't run on secured mode
    settings.Credentials = credentials;
    String mongoHost = "127.0.0.1";
    MongoServerAddress address = new MongoServerAddress(mongoHost);
    settings.Server = address;

    MongoClient client = new MongoClient(settings); 

similar: Error on MongoDB Authentication

0

I was also having the same problem and in my case as I read the server log.Its the problem because of the "authSchema version" of mongo so all you have to follow are the following steps and it'll start working

  • Restart the mongod without --auth option
  • Now use the following on the client

    mongo
    use admin;
    db.system.users.remove({})    
    db.system.version.remove({})
    db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 })
    

*create your user again and restart mongod in --auth mode

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.