Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to authenticate when using MongoClientSettings

I am trying to connect to a MongoDb database.

  • My client: ASP.NET Core 2 with MongoDb.Driver 2.6.1
  • My server: MongoDb 3.6.5 Community Edition, stand alone (no replica set), 64-bit on Windows Server 2016.

Authentication by connection string is working:

public MongoDbContext(AppSettings appSettings)
{
    var connectionString = "mongodb://myUsername:[email protected]:27017/myDatabaseName";

    _client = new MongoClient(connectionString);

    // ...
}

Authentication by MongoClientSettings is NOT working:

public MongoDbContext(AppSettings appSettings)
{
    var credentials = MongoCredential.CreateMongoCRCredential(databaseName: "myDatabaseName", username: "myUsername", password: "myPassword");
    var server = new MongoServerAddress(host: "myDomain.com", port: 27017);

    var mongoClientSettings = new MongoClientSettings
    {
        Credential = credentials,
        Server = server,
        ConnectionMode = ConnectionMode.Standalone,
        ServerSelectionTimeout = TimeSpan.FromSeconds(3)
    };

    _client = new MongoClient(mongoClientSettings);

   // ...
}

TimeoutException: A timeout occured after 3000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : "1", ConnectionMode : "Standalone", Type : "Standalone", State : "Disconnected", Servers : [{ ServerId: "{ ClusterId : 1, EndPoint : "Unspecified/myDomain.com:27017" }", EndPoint: "Unspecified/myDomain.com:27017", State: "Disconnected", Type: "Unknown", HeartbeatException: "MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate username 'myUsername' on database 'myDatabaseName'. ---> MongoDB.Driver.MongoCommandException: Command authenticate failed: auth failed.

I have been using the exact same values for both approaches (upper and lower case as well). But only the MongoClientSetting approach is throwing an exception. For different reasons though, I would rather use the MongoClientSetting approach than the connection string based approach.

Am I missing anything or is this a bug in the MongoDb Driver for C#?

To avoid misunderstandings: In the samples above I exchanged all real values (username, password, etc.) for fake values.

Thanks for any help!

like image 585
Ingmar Avatar asked May 28 '18 12:05

Ingmar


People also ask

How do I use MongoClientSettings?

To create a MongoClientSettings object, use the MongoClientSettings. builder() method and chain methods to specify your settings. After chaining them, use the build() method to create the MongoClientSettings object. Adds a listener for command events.

What is scram in MongoDB?

Salted Challenge Response Authentication Mechanism (SCRAM) is the default authentication mechanism for MongoDB. When a user authenticates themselves, MongoDB uses SCRAM to verify the supplied user credentials against the user's name , password and authentication database .


1 Answers

This is an authentication issue related to the call to MongoCredential.CreateMongoCRCredential, which creates the credential using MONGODB-CR (challenge-response) authentication mechanism.

Tracked the provided exception down to the MongoDBCRAuthenticator

While deprecated as of MongoDB 3.6, the original question indicated using MongoDb.Driver 2.6.1

However, the working connection string does not have any authMechanism authentication options which would imply that it would use the default authentication mechanism.

Use MongoCredential.CreateCredential to create a default credential similar to what worked using the connection string

var credentials = MongoCredential.CreateCredential(
    databaseName: "myDatabaseName", 
    username: "myUsername", 
    password: "myPassword"
);
like image 187
Nkosi Avatar answered Oct 28 '22 11:10

Nkosi