将authenticationDatabase参数传递给mongo C#驱动程序

问题描述 投票:0回答:1

以下命令行对我有用:

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)
...

我假设身份验证数据库存在问题,因为当我按如下方式使用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

c# mongodb mongodb-.net-driver
1个回答
2
投票

尝试此连接字符串:

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%清晰

© www.soinside.com 2019 - 2024. All rights reserved.