简体   繁体   English

在Mongo .NET 2.0驱动程序中捕获MongoAuthenticationException

[英]Catch MongoAuthenticationException in Mongo .NET 2.0 Driver

I'm doing MongoDB project based on .NET 2.0 driver which involves authentication to MongoDB. 我正在做基于.NET 2.0驱动程序的MongoDB项目,该项目涉及对MongoDB的身份验证。 There is a example code for what i'm doing: 我正在做的是一个示例代码:

public static bool createConneciton(string login, SecureString pass, string authDB) {
    var settings = new MongoClientSettings {
        Credentials = new[] {
            MongoCredential.CreateCredential(authDB, login, pass)
        },
        Server = new MongoServerAddress("my.mongodb.server", 27017)
    };
    mongoClient = new MongoClient(settings);
    return true;
}

if (Mongo.createConneciton(textBoxUsername.Text, pass, textBoxAuthDatabase.Text))
    Task<BsonDocument> results = Mongo.getNodeStats();

public static async Task<BsonDocument> getNodeStats() {
    try {
        var db = Mongo.mongoClient.GetDatabase("admin");
        var command = new BsonDocument {
            {"serverStatus",1}
        };
        BsonDocument result = await db.RunCommandAsync<BsonDocument>(command).ConfigureAwait(false);
                return result;
    }
    catch (Exception ex)
    {
        Logging.Log(ex);
        return null;
    }
}

Main problem i encountered so far is processing user's credentials. 到目前为止我遇到的主要问题是处理用户的凭据。 Because all operations are lazy and connection opens only on execution in getNodeStats() method. 因为所有操作都是惰性的,只有在getNodeStats()方法中执行时才会打开连接。 So if user types wrong credentials, he is going to wait for 30 seconds because instead of MongoDB.AuthenticationException or even MongoDB.ConnectionException method going to though only System.Timeout exception. 因此,如果用户键入错误的凭据,他将等待30秒,因为而不是MongoDB.AuthenticationException甚至MongoDB.ConnectionException方法只会进入System.Timeout异常。 If you look though text of exception that is quite obvious that both are rised but not catched. 如果你看一下异常的文本很明显,它们都会升级而不会被捕获。

"MongoDB.Driver.MongoConnectionException: An exception occurred while opening a connection to the server. ---> MongoDB.Driver.MongoAuthenticationException: Unable to authenticate using sasl protocol mechanism SCRAM-SHA-1

My first thought was to force open connection to check for credentials as soon as user typed them and hit connect button rather then waiting for any command to be executed, but apparently MongoClient class does not have .Open() method anymore. 我的第一个想法是强制打开连接,一旦用户键入它们并检查连接按钮而不是等待执行任何命令,就检查凭据,但显然MongoClient类不再具有.Open()方法。 So if it does not seem to be possible i at least would like to catch AuthenticationException without need to wait for timeout, but out of ideas where should i try and catch it. 因此,如果它似乎不可能,我至少想要捕获AuthenticationException而不需要等待超时,但出于想法我应该尝试捕获它。

You cannot connect mongodb using MongoCredential.CreateCredential .You have to use MongoCredential.CreateMongoCRCredential method to connect the db. 您无法使用MongoCredential.CreateCredential连接mongodb。您必须使用MongoCredential.CreateMongoCRCredential方法连接数据库。 Because the former credential use SCRAM-SHA-1 mechanism to connect db, in .NET which will fail. 因为前一个凭证使用SCRAM-SHA-1机制来连接db,所以在.NET中会失败。 And the reason I have not make clear. 我没有说清楚的原因。

Using MongoCredential.CreateMongoCRCredential , you have change "authSchema" setting in mongodb. 使用MongoCredential.CreateMongoCRCredential ,您可以在mongodb中更改“authSchema”设置。 You can refer to MongoDB-CR Authentication failed 您可以参考MongoDB-CR身份验证失败

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

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