I am trying to connect to a MongoDb database.
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!
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.
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 .
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"
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With