简体   繁体   English

MongoDB C# 身份验证异常

[英]MongoDB C# authentication exception

My database is hosted in mlab.com and I tried to connect it using mongodb C# driver but I am getting following exception when I try to connect to the mlab database.我的数据库托管在 mlab.com 中,我尝试使用 mongodb C# 驱动程序连接它,但是当我尝试连接到 mlab 数据库时出现以下异常。

MongoCommandException: Command 'saslStart' failed: Authentication failed. 
(response: { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." })   
MongoDB.Driver.Operations.CommandOperation_\`1[MongoDB.Driver.CommandResult].Execute (MongoDB.Driver.Internal.MongoConnection connection)
MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.RunCommand (MongoDB.Driver.Internal.MongoConnection connection, System.String databaseName, IMongoCommand command) 
MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.Authenticate (MongoDB.Driver.Internal.MongoConnection connection, MongoDB.Driver.MongoCredential credential)
Rethrow as MongoSecurityException: Error: 18 - Authentication failed.

MongoDB.Driver.Communication.Security.SaslAuthenticationProtocol.Authenticate (MongoDB.Driver.Internal.MongoConnection connection, 
MongoDB.Driver.MongoCredential credential) 
MongoDB.Driver.Communication.Security.Authenticator.Authenticate (MongoDB.Driver.MongoCredential credential) 
MongoDB.Driver.Communication.Security.Authenticator.Authenticate () 
MongoDB.Driver.Internal.MongoConnection.Open () 
MongoDB.Driver.Internal.MongoConnection.GetNetworkStream () 
MongoDB.Driver.Internal.MongoConnection.SendMessage (MongoDB.Bson.IO.BsonBuffer buffer, Int32 requestId)
Rethrow as MongoConnectionException:
Unable to connect to server dsXXXXXX.mlab.com:XXXXX: Error: 18 - Authentication failed.. 

MongoDB.Driver.Internal.DirectMongoServerProxy.Connect (TimeSpan timeout, MongoDB.Driver.ReadPreference readPreference)
MongoDB.Driver.MongoServer.Connect (TimeSpan timeout) MongoDB.Driver.MongoServer.Connect ()

Below is the line which throws the error.下面是引发错误的行。

client = new MongoClient (new MongoUrl("mongodb://:@dsxxxxx.mlab.com:xxxxx/mydb"));

And the db user has admin rights as well...username and password are also correct..Need some help :-)并且 db 用户也具有管理员权限...用户名和密码也正确...需要一些帮助 :-)

I use我用

var connStr = "mongodb://[username]:[password]@dsxxxx.mongolab.com:xxxx/[dbname]"
IMongoClient client = new MongoClient(connStr);

Notice mongolab.com instead of mlab.com, i just logged into mlab and the suggested connection string uses mongolab.com for me, so double check yours.请注意 mongolab.com 而不是 mlab.com,我刚刚登录到 mlab 并且建议的连接字符串为我使用 mongolab.com,因此请仔细检查您的。 You should see them as soon as you log into mlab and select your database.一旦您登录到 mlab 并选择您的数据库,您就会看到它们。

Note that the connection string will be different if you are connecting to a replicaset请注意,如果您连接到副本集,连接字符串将有所不同

Thanks, followed pieperu's advice. 谢谢,听从了Pieperu的建议。 Set up another set of credential for the DB (you can find beside Collections tab). 为数据库设置另一组凭据(可以在“集合”选项卡旁边找到)。 Appending those newly set credentials to connStr works. 将这些新设置的凭据附加到connStr上即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM