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"
                        }
                ]
Davita
  • 8,928
  • 14
  • 67
  • 119
  • Where the port 27017 is set? – Hamlet Hakobyan Jan 02 '15 at 19:32
  • You connect to localhost from the command line, but to 192.168.0.100 from C# - oversight or on purpose? – Eugen Rieck Jan 02 '15 at 19:33
  • @HamletHakobyan I tried already, no difference – Davita Jan 02 '15 at 19:34
  • What you have tried? I only ask the question. – Hamlet Hakobyan Jan 02 '15 at 19:34
  • 2
    @Davita it's not a connectivity issue.. it's an authentication one. – i3arnon Jan 02 '15 at 19:34
  • @EugenRieck It's same. The problem is not in db connection. The driver connects to mongodb without problem. It's authentication problem – Davita Jan 02 '15 at 19:35
  • @l3arnon I'm aware of that. Thanks :). Do you have any idea what that issue could be? :) – Davita Jan 02 '15 at 19:35
  • @Davita I can only guess that the credentials are wrong. Maybe this user can authenticate to `master` and not `agp`? – i3arnon Jan 02 '15 at 19:41
  • @l3arnon no, credentials are correct. See my last update – Davita Jan 02 '15 at 19:45
  • I do not understand what led You to believe that You have correct credentials. All You presented makes it clear that they are wrong. I can only hope that passwords You presented (MyPassword and MyPWD) are replaced for needs of this question and that the fact that they differ is NOT the only reason You have this problem. – Grzegorz W Jan 02 '15 at 19:51
  • @GrzegorzW yes they are placeholders :| do you mind sharing what's wrong with my setup? – Davita Jan 02 '15 at 19:55
  • 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. – Craig Wilson Jan 02 '15 at 21:10
  • @Davita, have you found the solution? I've tried alot and found nothing – Disposer Jan 10 '15 at 17:49
  • @Disposer Have a look at http://stackoverflow.com/a/28858440/31505 – Serhat Ozgel Mar 04 '15 at 15:35

2 Answers2

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

Kev
  • 121
  • 1
  • 7
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

Vikas
  • 432
  • 5
  • 18