簡體   English   中英

MongoDB C# 身份驗證異常

[英]MongoDB C# authentication exception

我的數據庫托管在 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 ()

下面是引發錯誤的行。

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

並且 db 用戶也具有管理員權限...用戶名和密碼也正確...需要一些幫助 :-)

我用

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

請注意 mongolab.com 而不是 mlab.com,我剛剛登錄到 mlab 並且建議的連接字符串為我使用 mongolab.com,因此請仔細檢查您的。 一旦您登錄到 mlab 並選擇您的數據庫,您就會看到它們。

請注意,如果您連接到副本集,連接字符串將有所不同

謝謝,聽從了Pieperu的建議。 為數據庫設置另一組憑據(可以在“集合”選項卡旁邊找到)。 將這些新設置的憑據附加到connStr上即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM