Hangfire MongoDB .Net Core 无法使用 sasl 协议机制 SCRAM-SHA-1 进行身份验证

Hangfire MongoDB .Net Core Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1

我正在使用 .Net Core 3.1 和 Hangfire MongoDb 进行后台调度,现在启动时抛出

Autofac.Core.DependencyResolutionException: An exception was thrown while activating λ:Hangfire.IGlobalConfiguration.
 ---> 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.Execute(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken

我在配置文件中的连接字符串是正确的,我能够通过 Mongo 客户端

从相同的连接字符串连接到数据库
"ConnectionStrings": {
"MongoJobSchedulerConnection": "mongodb://user:password@ipaddress:port/DbName"
}

这就是我在 Startup.cs

中添加 Hangfire 的方式
var mongoUrlBuilder = new MongoUrlBuilder(configuration.GetConnectionString("MongoJobSchedulerConnection"));
            var mongoClient = new MongoClient(mongoUrlBuilder.ToMongoUrl());
            

            services.AddHangfire((sp,configuration) => configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseActivator<JobActivator>(new SchedulerJobActivator(sp.GetRequiredService<IServiceScopeFactory>()))
                .UseMongoStorage(mongoClient, mongoUrlBuilder.DatabaseName, new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        MigrationStrategy = new MigrateMongoMigrationStrategy(),
                        BackupStrategy = new CollectionMongoBackupStrategy()
                    },
                    Prefix = "SchedulerQueue",
                    CheckConnection = true
                })
            );

关于这个问题的任何提示都会有很大的帮助

谢谢

原因是 MongoDB 以某种方式没有针对管理数据库进行身份验证 这可以通过将身份验证源添加到连接字符串来解决,例如

"ConnectionStrings": {
"MongoJobSchedulerConnection": "mongodb://user:password@ipaddress:port/DbName?authSource=admin"
}

或者可以在 MongoUrlBuilder 中指定,例如

var mongoUrlBuilder = new MongoUrlBuilder(configuration.GetConnectionString("MongoJobSchedulerConnection"));
mongoUrlBuilder.AuthenticationSource = "admin";