AspNetCore.Identity.Mongo icon indicating copy to clipboard operation
AspNetCore.Identity.Mongo copied to clipboard

Cannot connect to MongoDb when using authentication (on a remote server)

Open abhigantech opened this issue 3 years ago • 1 comments

I'm using latest MongoDb server that is installed on a remote windows server (2019), I'm using following settings in MongoDb config file:

network interfaces

net: port: 27017 bindIp: 0.0.0.0

security: authorization: enabled

When I connect to this server using MongoDb Compass it works fine, I can also connect to it using MongoDb C# driver, but for some reason AspNetCore.Identity.Mongo cannot connect to it with authentication (it works fine when I disable the authorization setting in config.)

I'm using following code:

builder.Services.AddIdentityMongoDbProvider<UserLogin, MongoUserRole, string>(identity =>
    {
        identity.Password.RequiredLength = 8;
        // other options
    },
    mongo =>
    {
        mongo.ConnectionString = "mongodb://userName:password@serverIP:27017/dbName?authMechanism=SCRAM-SHA-1";
        // other options
    });

Expected behavior It should connect

But it throws following error: 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.CommandUsingQueryMessageWireProtocol1.ProcessReply(ConnectionId connectionId, ReplyMessage1 reply) at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol1.Execute(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandWireProtocol1.Execute(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken) --- End of inner exception stack trace --- at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken) at MongoDB.Driver.Core.Authentication.AuthenticationHelper.Authenticate(IConnection connection, ConnectionDescription description, IReadOnlyList1 authenticators, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.ConnectionInitializer.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelper(CancellationToken cancellationToken) at MongoDB.Driver.Core.Connections.BinaryConnection.Open(CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.PooledConnection.Open(CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedInternal(CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedOrReuse(CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionHelper.AcquireConnection(CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnection(CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.Server.GetChannel(CancellationToken cancellationToken) at MongoDB.Driver.Core.Bindings.ServerChannelSource.GetChannel(CancellationToken cancellationToken) at MongoDB.Driver.Core.Bindings.ChannelSourceHandle.GetChannel(CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.RetryableReadContext.Initialize(CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.RetryableReadContext.Create(IReadBinding binding, Boolean retryRequested, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.FindOperation1.Execute(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.<>c__DisplayClass46_01.<FindSync>b__0(IClientSessionHandle session) at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSession[TResult](Func2 func, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.FindSync[TProjection](FilterDefinition1 filter, FindOptions2 options, CancellationToken cancellationToken) at MongoDB.Driver.FindFluent2.ToCursor(CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource1 source, CancellationToken cancellationToken) at AspNetCore.Identity.Mongo.Migrations.Migrator.Apply[TUser,TRole,TKey](IMongoCollection1 migrationCollection, IMongoCollection1 usersCollection, IMongoCollection1 rolesCollection) at AspNetCore.Identity.Mongo.MongoIdentityExtensions.AddIdentityMongoDbProvider[TUser,TRole,TKey](IServiceCollection services, Action1 setupIdentityAction, Action1 setupDatabaseAction, IdentityErrorDescriber identityErrorDescriber) at Program.<Main>$(String[] args) in C:\Users\admin\Documents\Abhishek\github\carebytes\Carebytes.Web\Program.cs:line 32

abhigantech avatar Aug 10 '22 07:08 abhigantech

hi @abhigantech , I'll ask you to check few thing from your side

  1. try to specify authSource in the url e.g. mongodb://userName:password@serverIP:27017/dbName?authMechanism=SCRAM-SHA-1&authSource=admin (usually it's admin, but you should set db name where user was created)
  2. make sure you user can login using authMechanism=SCRAM-SHA-1. You can find it in the credentials field in the user record (system.users)
  3. make sure you use correct username/pwd & ip/port

vova3211 avatar Aug 26 '24 09:08 vova3211