Connection string is not valid error

Olivia Bates 0 Reputation points
2024-02-29T21:07:53.4033333+00:00

I've set up a free cosmos DB for mongoDB vcore database without any issues, I've then been able to utilise the provided connection string to connect to the database with my credentials on both MongoDBCompass and also azure data studio which confirms the string is working and allows access to the database in question. However, when I have then used the given string in my .Net 8 C# application with the correct username and password inserted I get the following error every time it tries to set up the client:

MongoDB.Driver.MongoConfigurationException: The connection string 'mongodb+srv://<hidden>@mongodb-test.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000' is not valid.

The code to populate the client is:

  public NetworkService(IOptions<MongoDBSettings> mongoDBSettings)
 {
	_client = new MongoClient(mongoDBSettings.Value.ConnectionLocal);
 }

I've double checked the mongoDBSettings.Value.ConnectionLocal property and this is populated with the correct string string given by azure cosmos db so I'm uncertain as to why I'm getting this error saying the string is not valid. This code also executes perfectly fine and APIs all work as expected when I replace the string to point at my local mongodb database: "ConnectionLocal": "mongodb://localhost:27017/",

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,605 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Olivia Bates 0 Reputation points
    2024-02-29T21:34:45.4766667+00:00

    Figured out what the issue was - my password had an @ symbol in it and it looks like the client set up was then failing as it took this symbol to signify the end of the password ahead of the expected @ Encoding the symbol as %40 fixed the issue.


  2. Oury Ba-MSFT 18,601 Reputation points Microsoft Employee
    2024-02-29T22:34:40.7666667+00:00

    @Olivia Bates
    Issue: Seems like you are facing an issue with the MongoDB connection string when using mongodb+srv format in your .NET 8 C# application. Resolution: User password had an @ symbol in it and it looks like the client set up was then failing as it took this symbol to signify the end of the password ahead of the expected @ Encoding the symbol as %40 fixed the issue. Appreciate if you can mark this as accept answer. It will be beneficial for other community members with the same issue. Regards, Oury


  3. Pinaki Ghatak 3,830 Reputation points Microsoft Employee
    2024-03-01T19:48:07.47+00:00

    Hello @Olivia Bates

    The error message you’re seeing is usually due to an issue with the connection string format. Here are a few things to consider:

    SRV Record: The mongodb+srv option fails if there is no available DNS with records that correspond to the hostname identified in the connection string.

    If you use the +srv connection string modifier, the tls (or the equivalent ssl) option is set to true for the connection

    Special Characters: If your username or password contains special characters such as :, /, ?, #, [, ], or @, they must be converted using percent encoding

    For example, if your password is pass@word, it should be encoded as pass%40word in the connection string

    MongoDB Version: The connection string that allows for the mongodb+srv protocol was introduced for MongoDB 3.6]

    If your MongoDB instance is of this version or higher, then you must update your MongoDB driver to a current version.

    ConnectionString Format: Ensure that your connection string is in the correct format. For MongoDB, it should be in the format mongodb://username:password@host:port


    If this information provided here helps solve your issue, please tag this as answered, so it helps further community readers, who may have similar questions.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.