繁体   English   中英

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";

暂无
暂无

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

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