1.首先mongdb要以auth的方式启动
mongod --dbpath=D:mongodbdb --logpath=D:mongodblog.txt --install --serviceName "MongoDB" --auth
2.给mongodb增加用户名密码
mongo 127.0.0.1:27017/test
db.addUser("sa","123")
3.c#中配置连接字符串 即可连接
var connectionString = "mongodb://sa:123@127.0.0.1:27017/test";
var client = new MongoClient(connectionString);
var server = client.GetServer();