MongoDB.Driver.Database.Authenticate C# (CSharp) Метод

Authenticate() публичный Метод

public Authenticate ( string username, string password ) : bool
username string
password string
Результат bool
        public bool Authenticate(string username, string password)
        {
            Document nonceResult = this.SendCommand("getnonce");
            String nonce = (String)nonceResult["nonce"];

            if (nonce == null){
                throw new MongoException("Error retrieving nonce", null);
            }

            string pwd = Database.Hash(username + ":mongo:" + password);
            Document auth = new Document();
            auth.Add("authenticate", 1.0);
            auth.Add("user", username);
            auth.Add("nonce", nonce);
            auth.Add("key", Database.Hash(nonce + username + pwd));
            try{
                this.SendCommand(auth);
                return true;
            }catch(MongoCommandException){
                return false;
            }
        }

Usage Example

        public void TestAdminLogin()
        {
            bool ok = admindb.Authenticate(adminuser, adminpass);

            Assert.IsTrue(ok);
            admindb.Logout();
        }
All Usage Examples Of MongoDB.Driver.Database::Authenticate