Search code examples
c#mongodbmongodb-.net-driver

Passing authenticationDatabase parameter to mongo C# driver


Following command line works for me:

mongo -u myUsername -p myPassword --authenticationDatabase myAuthDb
use myDb
db.myCollection.find({})

However I cannot seem to authenticate properly from C#. My code:

var connectionString = "mongodb://myUsername:myPassword@localhost:27017/myAuthDb";
var mongoSettings = MongoClientSettings.FromConnectionString(connectionString);
var mongoClient = new MongoDB.Driver.MongoClient(mongoSettings);
mongoClient.GetDatabase("myDb").GetCollection("myCollection").FindAsync(...);

Throws the following exception:

MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
 ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellat
ionToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
...

I'm assuming the issue is with the authenticationDatabase since when I'm using the admin db as follows, everything works fine.

mongo -u root -p rootPassword
use myDb
db.myCollection.find({})
var connectionString = "mongodb://root:rootPassword@localhost:27017";
var mongoSettings = MongoClientSettings.FromConnectionString(connectionString);
var mongoClient = new MongoDB.Driver.MongoClient(mongoSettings);
mongoClient.GetDatabase("myDb").GetCollection("myCollection").FindAsync(...);

I'm using recent versions.

Mongo version 4.2

Mongo C# driver version 2.10.2


Solution

  • Try this connection string:

    var connectionString = "mongodb://myUsername:myPassword@localhost:27017/myDb?authSource=myAuthDb";
    

    or without initial Database:

    var connectionString = "mongodb://myUsername:myPassword@localhost:27017?authSource=myAuthDb";
    

    The Connection String URI Format documentation is not 100% clear regarding authSource