简体   繁体   English

MongoDB C#驱动程序MongoCredential对象

[英]MongoDB C# Driver MongoCredential object

The documentation for the MongoDB Driver seems to differ from the actual driver pulled from NuGet. MongoDB驱动程序的文档似乎与从NuGet中提取的实际驱动程序不同。

More specifically, the documented "MongoCredentials" (plural) doesn't exist, but only "MongoCredential" (singular). 更具体地说,记录的“MongoCredentials”(复数)不存在,但只有“MongoCredential”(单数)。 Further, MongoServer.GetDatabase doesn't seem to have a constructor that accepts MongoCredential, only MongoDatabaseSettings (alongside a string that names the databae), and I see no apparent way of giving a MongoDatabaseSettings object a MongoCredential object. 此外,MongoServer.GetDatabase似乎没有接受MongoCredential的构造函数,只有MongoDatabaseSettings(以及命名数据库的字符串),并且我没有看到明显的方式为MongoDatabaseSettings对象提供MongoCredential对象。

I haven't found any examples on Google with the objects I'm finding in the driver, only ones that align with the (outdated?) official documentation. 我没有在Google上找到我在驱动程序中找到的对象的任何示例,只有那些与(过时的?)官方文档一致的对象。

The driver that I'm using is called (in the NuGet Package Manager) "Official MongoDB C# driver." 我正在使用的驱动程序(在NuGet包管理器中)被称为“官方MongoDB C#驱动程序”。

To summarize: How does one actually provide credentials in the C# driver? 总结一下:如何在C#驱动程序中实际提供凭据?

I'm also using the Official MongoDB C# driver from NuGet, version 1.8.3. 我也在使用NuGet的官方MongoDB C#驱动程序 ,版本1.8.3。

Indeed, the CSharp Driver Tutorial seems outdated. 实际上, CSharp驱动程序教程似乎已经过时了。

However, the API documentation is correct; 但是, API文档是正确的; there's an entry there for the MongoCredential class (singular). 那里有MongoCredential类的入口(单数)。

You can create a credential using either the constructor or one of the static factory methods ( CreateGssapiCredential or CreateMongoCRCredential ). 您可以使用构造函数或其中一个静态工厂方法( CreateGssapiCredentialCreateMongoCRCredential )创建凭证。

Next, in order to use the credentials you cannot specify them in the GetDatabase() call, but earlier, when you create the Server , like so: 接下来,为了使用凭据,您无法在GetDatabase()调用中指定它们,但是在创建服务器时更早,如下所示:

var db1Credential = MongoCredential.CreateMongoCRCredential("db1", "uid", "pwd");
var db2Credential = MongoCredential.CreateMongoCRCredential("db2", "uid", "pwd");

var server = new MongoServer(
    new MongoServerSettings
        {
            Server = new MongoServerAddress("localhost", 27017),
            Credentials = new[]
                            {
                                db1Credential,
                                db2Credential
                            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM