【问题标题】:Passing authenticationDatabase parameter to mongo C# driver将 authenticationDatabase 参数传递给 mongo C# 驱动程序
【发布时间】:2020-02-25 15:31:29
【问题描述】:

以下命令行对我有用:

mongo -u myUsername -p myPassword --authenticationDatabase myAuthDb
use myDb
db.myCollection.find({})

但是我似乎无法从 C# 正确地进行身份验证。我的代码:

var connectionString = "mongodb://myUsername:myPassword@localhost:27017/myAuthDb";
var mongoSettings = MongoClientSettings.FromConnectionString(connectionString);
var mongoClient = new MongoDB.Driver.MongoClient(mongoSettings);
mongoClient.GetDatabase("myDb").GetCollection("myCollection").FindAsync(...);

抛出以下异常:

MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
 ---> MongoDB.Driver.MongoCommandException: Command saslStart failed: Authentication failed..
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.AuthenticateAsync(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableReadContext.CreateAsync(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellat
ionToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
...

我假设问题出在 authenticationDatabase 上,因为当我如下使用 admin db 时,一切正常。

mongo -u root -p rootPassword
use myDb
db.myCollection.find({})
var connectionString = "mongodb://root:rootPassword@localhost:27017";
var mongoSettings = MongoClientSettings.FromConnectionString(connectionString);
var mongoClient = new MongoDB.Driver.MongoClient(mongoSettings);
mongoClient.GetDatabase("myDb").GetCollection("myCollection").FindAsync(...);

我正在使用最新版本。

Mongo 4.2 版

Mongo C# 驱动程序版本 2.10.2

【问题讨论】:

  • 您使用的是什么版本的 MongoDB 和 c# 驱动程序?
  • @Jawad 谢谢,已编辑
  • 看看this solves your issue。向您展示如何使用 CreateCredential 使用 adminDB 设置身份验证

标签: c# mongodb mongodb-.net-driver


【解决方案1】:

试试这个连接字符串:

var connectionString = "mongodb://myUsername:myPassword@localhost:27017/myDb?authSource=myAuthDb";

或没有初始数据库:

var connectionString = "mongodb://myUsername:myPassword@localhost:27017?authSource=myAuthDb";

Connection String URI Format documentation 关于authSource 并不是 100% 清楚

【讨论】:

  • 我确信 authenticationDatabase 在 URI 中有效。对authSource 不积极。
  • authenticationDatabase 用作启动 mongo shell 的选项。对于 URI 连接字符串,它是 authSource
  • 有趣的是,我的身份验证已经与authenticationDatabase 合作了 5 年。深入研究后,如果您不指定默认数据库,似乎使用了 admin 。 github.com/mongodb/mongo-csharp-driver/blob/master/src/…TIL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
相关资源
最近更新 更多