![](/img/trans.png)
[英]MongoDB.Driver.MongoAuthenticationException C# connection
[英]c# MongoAuthenticationException when connecting to remote mongodb (3.2)
我正在尝试使用以下C#代码连接到远程mongodb(3.2版)服务器:
_client = new MongoClient(
new MongoClientSettings
{
Credentials = new[]
{
MongoCredential.CreateMongoCRCredential("test", "usernamex", "password")
},
Server = new MongoServerAddress("xx.xx.xx.xxx", 27017)
});
var databases = _client.ListDatabasesAsync().Result;
它引发了以下异常:MongoAuthenticationException - 无法在数据库“test”上验证用户名“usernamex”。 在远程服务器上,用户“usernamex”存在。 在远程服务器上,netstat -a -n建立了xx.xx.xx.xxx:27017连接。 任何想法?
对于今后面临这样一个问题的所有人来说,解决方案是:
确保连接字符串中的用户确实具有足够的权限。 使用mongo shell它将是:> use test> db.getUser(“usernamex”)
替换以下代码行:
MongoCredential.CreateMongoCRCredential(“test”,“usernamex”,“password”)
这一个:
MongoCredential.CreateCredential("test", "usernamex", "password")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.