6

While setting up my new environment with a freshly deployed MongoDB container with authentication enabled, I ran into this exception: "An unhandled exception has occurred while executing the request. MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> System.NotSupportedException: Unable to create an authenticator."

In my case I'm using a connection string like this example: mongodb://USER:PASSWORD@HOST:27017/?authMechanism=DEFAULT. This string works perfectly fine in MongoDB Compass but not inside my .NET 6.0 application.

4 Answers 4

21

If you take a look at the source code of C# MongoDB driver in MongoCredential.cs#L469, you see this exception gets thrown while checking the auth mechanism.

After specifying the exact auth mechanism in the connection string, all exceptions are gone!

example: mongodb://USER:PASSWORD@HOST:27017/?authMechanism=SCRAM-SHA-256

Hope anyone else googeling around will find my answer helpful!

happy coding.

Sign up to request clarification or add additional context in comments.

Comments

9

Remove the authMechanism=DEFAULT parameter from the connection string

2 Comments

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
Underrated answer
3

For me this just changed the exception to unable to authenticate using sasl protocol mechanism scram-sha-256

After some trial and error I got it working by changing the connection string to:

mongodb://USER:PASSWORD@HOST:27017/?authSource=admin

I have no idea why this was needed as the default and SCRAM-SHA-256 connectionstrings works fine in other contexts...

Comments

1

I had similar problems and found out that usernames that include _ just don't want to work with the C# driver.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
It's very strange but it seems resolved my problem, is there any document?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.