MongoDB C#驱动程序无法运行数据库命令验证
我想用C#而不是通过在MongoDB C#驱动程序无法运行数据库命令验证,c#,mongodb,authentication,mongodb-.net-driver,C#,Mongodb,Authentication,Mongodb .net Driver,我想用C#而不是通过在MongoClient()实例上传递连接字符串/凭据来验证MongoDB。就像我们在MongoDB Shell上做的那样,我们调用monog->db.auth(,)这意味着首先连接到数据库,然后进行身份验证 编写C#代码 这是我的代码: var mongoClient = new MongoClient(); var testDB = mongoClient.GetDatabase("test"); string username = txtUserName.Text;
MongoClient()
实例上传递连接字符串/凭据来验证MongoDB。就像我们在MongoDB Shell上做的那样,我们调用monog->db.auth(,)
这意味着首先连接到数据库,然后进行身份验证
这是我的代码:
var mongoClient = new MongoClient();
var testDB = mongoClient.GetDatabase("test");
string username = txtUserName.Text;
string password = txtPassword.Password;
// Check password
var cmd = new BsonDocument("authenticate", new BsonDocument
{
{"username",username },
{"password",password }
});
var queryResult = testDB.RunCommand<BsonDocument>(cmd);
var mongoClient=new mongoClient();
var testDB=mongoClient.GetDatabase(“测试”);
字符串username=txtUserName.Text;
字符串密码=txtPassword.password;
//检查密码
var cmd=new BsonDocument(“身份验证”),new BsonDocument
{
{“用户名”,用户名},
{“密码”,密码}
});
var queryResult=testDB.RunCommand(cmd);
我的代码连接到MongoDB并调用authenticate数据库命令()以使用它登录--auth
选项运行MongoDB我已经阅读了MongoDB文档(也是我在上面添加的链接),我找不到我缺少的内容。我想你可以使用
JsonCommand
调用eval
函数来执行db.auth
函数,就像这样-未测试-:
var command = new JsonCommand<BsonDocument>(@"{ eval: ""db.auth(\""username\"", \""password\"");"" }");
var result = db.RunCommand(command);
var command=newjsoncommand(@“{eval:”db.auth(\”“username\”、\”“password\”);“}”);
var result=db.RunCommand(command);
我认为您可以使用JsonCommand
调用eval
函数来执行db.auth
函数,如下所示-未测试-:
var command = new JsonCommand<BsonDocument>(@"{ eval: ""db.auth(\""username\"", \""password\"");"" }");
var result = db.RunCommand(command);
var command=newjsoncommand(@“{eval:”db.auth(\”“username\”、\”“password\”);“}”);
var result=db.RunCommand(command);
数据库主机名和url在哪里???默认情况下,它连接到localhost:27017。我们不需要指定主机名。哦。。好的。。谢谢我不熟悉c#驱动程序连接。在Java中,我们必须使用ServerAddress类指定主机名和url。虽然在C中类似,但默认情况下,它连接到localhost:27017。我们不需要指定主机名。哦。。好的。。谢谢我不熟悉c#驱动程序连接。在Java中,我们必须使用ServerAddress类指定主机名和url。我想这在C语言中是相似的#