Authentication Failure from Azure app service after upgrade to 2.19.1 using C# drivers

I recently updated the MongoDB.Driver library from 2.17 to 2.19.1.

The connection string I am using is formatted as,

mongodb+srv://USERNAME:PASSWORD@SERVER/DATABASE?retryWrites=true&w=majority

which is exactly the same as I was using prior to the update. I am able to connect locally using these connection details both from my code, and from mongoshell.

All was well until I deployed my site to an Azure app service and I started receiving this error in the logs

Exception Info: MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1.
 ---> MongoDB.Driver.MongoCommandException: Command saslContinue failed: bad auth : 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)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Authentication.SaslAuthenticator.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.DefaultAuthenticator.Authenticate(IConnection connection, ConnectionDescription description, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Authentication.AuthenticationHelper.Authenticate(IConnection connection, ConnectionDescription description, IReadOnlyList`1 authenticators, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.ConnectionInitializer.Authenticate(IConnection connection, ConnectionInitializerContext connectionInitializerContext, 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.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.FindOperation`1.Execute(IReadBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteReadOperation[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteReadOperation[TResult](IClientSessionHandle session, IReadOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass46_0`1.<FindSync>b__0(IClientSessionHandle session)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
   at AspNetCore.Identity.Mongo.Migrations.Migrator.Apply[TUser,TRole,TKey](IMongoCollection`1 migrationCollection, IMongoCollection`1 usersCollection, IMongoCollection`1 rolesCollection)
   at AspNetCore.Identity.Mongo.MongoIdentityExtensions.AddIdentityMongoDbProvider[TUser,TRole,TKey](IServiceCollection services, Action`1 setupIdentityAction, Action`1 setupDatabaseAction, IdentityErrorDescriber identityErrorDescriber)
   at AspNetCore.Identity.Mongo.MongoIdentityExtensions.AddIdentityMongoDbProvider[TUser,TRole](IServiceCollection services, Action`1 setupIdentityAction, Action`1 setupDatabaseAction)
   at Program.<Main>$(String[] args) in D:\a\1\s\Source\TournamentGo.Portal\Program.cs:line 85
   at Program.<Main>(String[] args)

Does anyonw know what might have changed between these versions that would cause this and what I need to change to resolve it?

Just to update, I’ve rolled it back to 2.17.1 and it didn’t resolve the issue so there is something else going on.

I’ve removed all the network restrictions for this server and it still hasn’t helped.

The database is hosted in Atlas, version 6.0.6

Hi, Glenn,

The error message bad auth : authentication failed. usually indicates incorrect credentials. I would suggest connecting via mongosh or Compass with those same credentials to eliminate that as a potential root cause. I would recommend verifying that your username and password do not contain any special characters that require percent-encoding. See Connection String components for more information.

Sincerely,
James

Thanks for the response James.

I’ve tried those things. Running the code locally with the same connection string as I’ve deployed with, then connecting using the username and password through mongoshell. These all work fine.

This connection string was working previously so something else must have changed.

The connection string is copied from atlas and the password also generated in Atlas so I would think that it complies with the password rules.

Aside from network, is there anything else which might be blocking the connection?

I’ve redeployed my last working version with the same connection and that still works.

So there must be something in the code. It is failing at start-up and the from the stack trace I can see it is failing inside the AspNetCore.Identity.Mongo library I am using so I’ll attach a debugger and start stepping through there.

I think there is little you could suggest at the moment. Will report back if I find any Mongo specific issues with this.

Issue solved.

It turned out to be a red herring. My deployments were picking up on a configuration file which had snuck into my deployment pipeline.

Thanks for your time.

Glad to hear that you found the root cause of your issue. Have a great weekend!

After updating the MongoDB.Driver library and deploying your site to Azure App Service, it seems there’s a connection issue. Here’s what you can do:

  1. Double-check your connection string for any mistakes.
  2. Make sure Azure App Service allows outbound connections to MongoDB.
  3. Check MongoDB server’s firewall settings.
  4. Review any changes in MongoDB.Driver’s documentation.
  5. Look into Azure App Service logs for detailed error messages.
  6. Test connecting to MongoDB locally on Azure.
  7. If needed, reach out to Azure support for assistance.