错误:mongoDB中的密码特殊字符问题
代码:
connstr="mongodb://test:test@123@67.186.193.192:27017/testdb"
这里我将密码“test @ 123”更改为“test%4123”。因为@ ASCII码是%4 现在工作正常。
MongoClient client = new MongoClient(connstr); //vss
var server = client.GetServer();
//server.Ping();
var db = client.GetDatabase(dbname);
List<KeyValuePair<string, string>> collections = new List<KeyValuePair<string, string>>();
var tableviewset = JArray.Parse(db.ListCollectionsAsync().Result.ToListAsync<BsonDocument>().Result.ToJson());
但是我的用户会使用不同的密码,比如$,^,*,(......等等。所以在这种情况下我做了什么?
答案 0 :(得分:0)
你可以做这样的事情,你可以使用MongoClientSettings来获取密码和所有其他参数。
private MongoClientSettings GetSecuritySettingForConnection()
{
MongoClientSettings settings = new MongoClientSettings();
settings.Server = new MongoServerAddress(host, port);
MongoIdentity identity = new MongoInternalIdentity(authenticationDatabase, userName);
MongoIdentityEvidence evidence = new PasswordEvidence(password);
settings.Credentials = new List<MongoCredential>()
{
new MongoCredential("SCRAM-SHA-1", identity, evidence)
};
settings.MaxConnectionIdleTime = new TimeSpan(0, 2, 0);
return settings;
}
之后你可以在这里执行此功能:
var client = new MongoClient(GetSecuritySettingForConnection());
如果有任何混淆,请告诉我。