【问题标题】:Hangfire MongoDB .Net Core Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1Hangfire MongoDB .Net Core 无法使用 sasl 协议机制 SCRAM-SHA-1 进行身份验证
【发布时间】:2021-06-01 07:39:01
【问题描述】:

我正在使用 .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 C# 驱动程序?你的 MongoDB 是什么版本?
  • @jawad MongoDb.Driver=2.10.3 和 MongoDb=4.2.4

标签: mongodb .net-core hangfire


【解决方案1】:

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

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

或者这可以在 MongoUrlBuilder 中指定

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

【讨论】:

    猜你喜欢
    • 2015-10-02
    • 2015-12-20
    • 2016-05-30
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2018-11-14
    • 2016-10-15
    相关资源
    最近更新 更多