'Is there a way to authenticate a MongoDB user before connection in a C# WinForm?
I'm playing with building a MongoDB Explorer in a Windows Form specific to our implementation and I'm looking for the C# equivalent of db.Auth(). I've found this here:
mongodb: check user/password with c# driver
It mentions the Authentication section of the MongoDB documentation, but little else and the question is 7 years old. Comments there mention Client.GetServer, but that's deprecated and gone. I can try to list databases or the like, but all that gets me is a TimeOut exception.
I built this out and it works, but seems a bit backwards:
public bool Authenticate(string userName, string password)
{
bool auth = false;
ProcessStartInfo si = new ProcessStartInfo(@"C:\yada\blah\mongo.exe")
{
Arguments = $" blah--authenticationDatabase admin -u {userName} -p {password}",
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden
};
Process process = new Process();
process.StartInfo = si;
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string output = process.StandardOutput.ReadLine();
if (output.StartsWith("Implicit session"))
{
process.Kill();
auth = true;
break;
}
}
return auth;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|