First, i use a standalone MongoDB server 4.09 under windows 10 and develop with Visual Studio Community 15.9.12. All my code works fine when authentification is not enabled. My goal is to make my code work with authentification enabled as it actually fails on creating index for collections.
Authentification works well (as exposed in the code below) and does not have troubles like in most other questions on this site like the ones with similar errors tile
"A timeout occured after 30000ms selecting a server using CompositeServerSelector" => A timeout occured after 30000ms selecting a server using CompositeServerSelector.
The problem comes when i try to create an index for a collection with a code like the one below.
I have to mention again that the code works well without authentification and that i added the role "dbOwner" for the used db and for the username authentified.
When Authentification is not enabled, i use something like this :
DBContext.client = new MongoClient(DBADRESS);
It works fine.
When Authentification is enabled, i use something like this :
DBContext.client = new MongoClient("mongodb://username:password@localhost:27017/MatchbookDB");
It works well too.
Problem with collection indexing =>
DBContext.collections[CollectionName].Indexes.CreateOne(indexModel);
In case you ask what is DBContext, it's declared like this :
public static (MongoClient client, IMongoDatabase database, Dictionary<string, IMongoCollection<BsonDocument>> collections,
Dictionary<string, string> Commands) DBContext;
The error message is too long but it begins with :
"A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = MongoDB.Driver.MongoClient+AreSessionsSupportedServerSelector, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. ...".
I'm surprised as i would have expected this kind of error at the authentification step, not later on. I was thinking it was because of lack of user role for this db but as i tried with "dbOwner" to give the user a high level of db rights but with no more success.
Having tsaid that, we have a little more info if we go a little deeper into the error listing :
"Client view of cluster state is { ClusterId : \"1\", ConnectionMode : \"ReplicaSet\", Type : \"ReplicaSet\", State : \"Disconnected\", Servers : [{ ServerId: \"{ ClusterId : 1, EndPoint : \"Unspecified/localhost:27017\" }\", EndPoint: \"Unspecified/localhost: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 using sasl protocol mechanism SCRAM-SHA-1. ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..\r\n à MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol
1.ProcessReply(ConnectionId connectionId, ReplyMessage
1 reply)\r\n à MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.d__14.MoveNext()\r\n---"
I already tried without success the solution given in some answers of the question given at the beginning, ie replacing the connection string method in like below.:
new MongoClient("mongodb://username:password@ds011111.mongolab.com:11111/db-name")