Abdul Moiz
Abdul Moiz

Reputation: 1327

Mongo connection not working with c# driver version 2.4.3

My code

    using MongoDB.Bson;
    using MongoDB.Driver;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace MongoDriver2._4._3Test
    {
        class Program
        {
            protected static MongoCredential _credentials;
            protected static MongoClientSettings mongoClientSettings;
            protected static IMongoClient _client;
            protected static IMongoDatabase _database;
            static void Main(string[] args)
            {
                _credentials = MongoCredential.CreateMongoCRCredential("dbName", "userName", "password");
                mongoClientSettings = new MongoClientSettings
                {
                    Credentials = new[] { _credentials },
                    Server = new MongoServerAddress("172.x.x.x", 27017)
                };
                _client = new MongoClient(mongoClientSettings);
                getDataFromMongo();
                Console.ReadLine();
            }
            static async void getDataFromMongo()
            {
                _database = _client.GetDatabase("dbName");
                var collection = _database.GetCollection<BsonDocument>("_project");
                var builder = Builders<BsonDocument>.Filter;
                var filter = new BsonDocument();
                var count = 0;
                using (var cursor = await collection.FindAsync(filter))
                {
                    while (await cursor.MoveNextAsync())
                    {
                        var batch = cursor.Current;
                        foreach (var document in batch)
                        {
                            Console.WriteLine(count + "_id -> " + document[0] + "ProjectName -> " + document[7] + "\n");
                            // process document
                            count++;
                        }
                    }
                }
            }
        }
    }

Issue:

{"A timeout occured after 30000ms selecting a server using CompositeServerSelector{ Selectors = ReadPreferenceServerSelector{ ReadPreference = { Mode : Primary } }, LatencyLimitingServerSelector{ AllowedLatencyRange = 00:00:00.0150000 } }. Client view of cluster state is { ClusterId : \"1\", ConnectionMode : \"Automatic\", Type : \"Unknown\", State : \"Disconnected\", Servers : [{ ServerId: \"{ ClusterId : 1, EndPoint : \"172.24.17.3:27017\" }\", EndPoint: \"172.24.17.3: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 'devTeam' on database 'BAC_Test'. ---> MongoDB.Driver.MongoCommandException: Command authenticate failed: auth failed.\r\n   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)\r\n   at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol`1.<ExecuteAsync>d__11.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Authentication.MongoDBCRAuthenticator.<AuthenticateAsync>d__7.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at MongoDB.Driver.Core.Authentication.MongoDBCRAuthenticator.<AuthenticateAsync>d__7.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Authentication.AuthenticationHelper.<AuthenticateAsync>d__1.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Connections.ConnectionInitializer.<InitializeConnectionAsync>d__3.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()\r\n   --- End of inner exception stack trace ---\r\n   at MongoDB.Driver.Core.Connections.BinaryConnection.<OpenHelperAsync>d__48.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()\r\n   at MongoDB.Driver.Core.Servers.ServerMonitor.<HeartbeatAsync>d__27.MoveNext()\" }] }."}

I am unable to connect to mongodb. My package version is 2.4.3 Mongodb official driver for c#, i tried both, connect using connection string

_client = new MongoClient("mongodb://user:[email protected]:27017/dbname");

and connect using mongoClientSettings as shown in the code given above

        _credentials = MongoCredential.CreateMongoCRCredential("dbName", "userName", "password");
                mongoClientSettings = new MongoClientSettings
                {
                    Credentials = new[] { _credentials },
                    Server = new MongoServerAddress("172.x.x.x", 27017)
                };
                _client = new MongoClient(mongoClientSettings);

unfortunately both are not working. I have already seen the solutions available MongoDB-CR Authentication failed which are asking me to downgrade authSchema version but i don't wan to do that. I have everything updated in my case i am using mongo 3.4 and c# driver 2.4.3 so i want a solution that is in accordance with the latest updates.

Upvotes: 0

Views: 1157

Answers (1)

Phani vikranth
Phani vikranth

Reputation: 108

Please see if the username is configured for this database.

Unable to authenticate username 'devTeam' on database 'BAC_Test'. may be the database it should authenticate is admin?

Try to authenticate individually like this.

MongoCredential.CreateCredential("admin", Username, Password)

let me know your findings

Thanks, Phanivikranth

Upvotes: 1

Related Questions