MongoDB connection

Hi, I have problem with connecting to MongoDB, on connecting this error is throwing:

MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. —> System.MissingMethodException: void System.Security.Cryptography.Rfc2898DeriveBytes…ctor(string,byte,int,System.Security.Cryptography.HashAlgorithmName)

I’m using .NET Framework 4.8.1 and MongoDB 2.19.1.
How I can fix this?

Hi, @LulaczTV,

Welcome to the MongoDB Community Forums. I understand that you’re receiving a MissingMethodException related to System.Security.Cryptography.Rfc2898DeriveBytes. Taking a look at the MSDN documentation on this particular constructor, it is part of .NET Framework 4.7.2, 4.8, and 4.8.1 as well as .NET Standard 2.1.

Rfc2898DeriveBytes is used by both SCRAM-SHA-1 and SCRAM-SHA-256 authenticators. These authenticators are commonly used with MongoDB for challenge-response authentication when a username/password are provided.

Please provide a self-contained repro of the issue including the csproj file so that we can investigate further.


1 Like

Hi @James_Kovacs,

I found out that problem is not on connecting but on finding document in collection.

Here’s code where problem appear:

                var client = new MongoClient("mongodb://username:password@ip:port/?authMechanism=SCRAM-SHA-1&authSource=admin&connectTimeoutMS=1000&socketTimeoutMS=1000&serverSelectionTimeoutMS=1000");
                var database = client.GetDatabase("test");
                var collection = database.GetCollection<PlayerModel>("playerdatas");
                //Finding document with player SteamID
                var playerFilter = Builders<PlayerModel>.Filter.Eq("SteamID", player.UserId);
               //here's problem
                var playerData = collection.Find(playerFilter).FirstOrDefault();

if it’s needed, I can upload whole code on github.
and here’s csproj file (I have no idea if I can send it here as file so i uploaded it)

Hi, @LulaczTV,

Thank you for providing the code sample and csproj file. I can see from the csproj file that you are using Unity. Unity performs code stripping to remove unreachable code. Unfortunately it can sometimes get it wrong, especially when code is referenced dynamically at runtime. I would recommend disabling code stripping and see if the problem is resolved. If so, you can try adjusting the level of stripping. You can also create a link.xml file that preserves particular types, methods, and properties.


I don’t really know if I can use it because i’m making plugin to the unity game. I tried before to make link.xml but idk if I did it correctly.

If you’re making a game plugin, I would typically recommend using Atlas App Services and/or the Realm .NET SDK rather than the MongoDB .NET/C# Driver.

The driver is designed and intended for server-side apps. It would require embedding credentials with read/write access into your plugin. If you need to rotate credentials for any reason, you would have to update all your clients. It is also a security risk as a malicious attacker could extract those credentials from your binary and use them to directly access and modify your data.

Using Atlas App Services and/or the Realm .NET SDK would allow you finer grained access control more appropriate for client-side apps. I strongly recommend investigating this approach for your plugin.


alright, I will try that, thank you so much for replying and help!

Realm looks good, but I have no idea how I can use that in my case. I need to connect database to my discord bot in JavaScript, to website in PHP and game plugin in c#, but I don’t really know from these docs how I can connect it together.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.