MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server

My Code:

MongoClientSettings mongoClientSettings = MongoClientSettings.FromConnectionString($"mongodb+srv://TRsgjE8NkQBt26xKnHqZ:RK9eUwz5vkpMcuLEtQ2C3ymHgYf8TnjJ6q7VGh4a@eastus-database.znjc3a3.mongodb.net/?authSource=%24external&authMechanism=MONGODB-AWS&retryWrites=true&w=majority&authMechanismProperties=AWS_SESSION_TOKEN:hVtHTqDNPufKjs4W92ECMGn3J7XAxFcZea5zSdpw6RQkrY8Lmyjnmv9swCW68LQukSFED5GJx4BURYVgacy7rXqKTPtH2pbdhM3eHZ2D3mucCGMQXSRezxTWrYJnELb7jUy6s8ktvNFgdKAa4pwq5Bf8LANmcvP9HKnGd7WQqDRXErxk5upswBatZyMbeSF2J3T6YzChvMGrKXuf6AmqFybTp4h3JnEgBSVtZj97cRazxWw52CskdQe8PUTNM4zJc98SHCjhDeLwfryFkUqu&appName=EastUS-Database");
        mongoClientSettings.MaxConnectionLifeTime = TimeSpan.FromHours(24);
        
        MongoClient mongoClient = new MongoClient(mongoClientSettings);

        IMongoCollection<UserAccountObject> accountCollection = mongoClient.GetDatabase("My_Database_Name").GetCollection<UserAccountObject>("My_Collection_Name");
        await accountCollection.InsertOneAsync(new UserAccountObject());

Hello, I am encountering a problem where I can access my MongoClient but I cannot use InsertOneAsync or InsertOne with an Object. I am using AWS IAM to replace the Mongo Database User for authentication but still using MongoDB Custom Roles to assign to AWS IAM. I don’t know where the problem originates from but I always get an error on the line await accountCollection.InsertOneAsync(new UserAccountObject()); and it reports an error at the end:

Even though I have tried to increase the time to 24 hours, it does not yield any results, and I don’t think the problem here is related to time because this error occurs immediately when the InsertOneAsync line is called. I have adjusted and gained access to MongoClient, previously I had an error when MongoClient could not connect due to some AWS STS being created invalid with MongoClient ConnectionString and I have used the loop method until MongoClient accepts one of the AWS STS created, I can be sure that AWS IAM has been added to Mongo Database User and has been granted Insert permission into that Mongo Database User. I hope I can get help soon, thank you.

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server.
 ---> System.Security.Authentication.AuthenticationException: Authentication failed because the remote party sent a TLS alert: 'InternalError'.
 ---> System.ComponentModel.Win32Exception (0x80090326): The message received was unexpected or badly formatted.
   --- End of inner exception stack trace ---
   at System.Net.Security.SslStream.ForceAuthenticationAsync[TIOAdapter](Boolean receiveFirst, Byte[] reAuthenticationData, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.SslStreamFactory.CreateStreamAsync(EndPoint endPoint, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at MongoDB.Driver.Core.Connections.BinaryConnection.OpenHelperAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.PooledConnection.OpenAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedInternalAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.ConnectionCreator.CreateOpenedOrReuseAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionHelper.AcquireConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.AcquireConnectionAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.GetChannelAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableWriteContext.InitializeAsync(CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableWriteContext.CreateAsync(IWriteBinding binding, Boolean retryRequested, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken)
   at MongoDB.Driver.OperationExecutor.ExecuteWriteOperationAsync[TResult](IWriteBinding binding, IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperationAsync[TResult](IClientSessionHandle session, IWriteOperation`1 operation, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.BulkWriteAsync(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionBase`1.InsertOneAsync(TDocument document, InsertOneOptions options, Func`3 bulkWriteAsync)

Hi, @Quan_Nguyen2,

Examining the exception, the connection failed because:

Authentication failed because the remote party sent a TLS alert: 'InternalError'. ---> System.ComponentModel.Win32Exception (0x80090326): The message received was unexpected or badly formatted.

This is indicative of a TLS handshake failure. It could be due to a variety of reasons. Notably it is coming from the operating system (hence the Win32Exception) failing to create a secure TLS channel with the cluster node. I would suggest checking your Windows System Event Logs for further information on the cause of the failure.

Sincerely,
James

1 Like

Oh my god, I forgot to add my IP Address to MongoDB and AWS VPC and that’s why it didn’t work, I didn’t expect that after so long I would make such a dumb mistake. Anyway, thank you very much :sweat_smile: