// public methods /// <summary> /// Authenticates the connection against the given database. /// </summary> /// <param name="connection">The connection.</param> /// <param name="credential">The credential.</param> public void Authenticate(MongoConnection connection, MongoCredential credential) { using (var conversation = new SaslConversation()) { var currentStep = _mechanism.Initialize(connection, credential); var command = new CommandDocument { { "saslStart", 1 }, { "mechanism", _mechanism.Name }, { "payload", currentStep.BytesToSendToServer } }; while (true) { CommandResult result; try { result = RunCommand(connection, credential.Source, command); } catch (MongoCommandException ex) { var message = "Unknown error occured during authentication."; var code = ex.CommandResult.Code; var errmsg = ex.CommandResult.ErrorMessage; if (code.HasValue && errmsg != null) { message = string.Format("Error: {0} - {1}", code, errmsg); } throw new MongoSecurityException(message, ex); } if (result.Response["done"].AsBoolean) { break; } currentStep = currentStep.Transition(conversation, result.Response["payload"].AsByteArray); command = new CommandDocument { { "saslContinue", 1 }, { "conversationId", result.Response["conversationId"].AsInt32 }, { "payload", currentStep.BytesToSendToServer } }; } } }
// public methods /// <summary> /// Authenticates the connection against the given database. /// </summary> /// <param name="connection">The connection.</param> /// <param name="credential">The credential.</param> public void Authenticate(MongoConnection connection, MongoCredential credential) { using (var conversation = new SaslConversation()) { var currentStep = _mechanism.Initialize(connection, credential); var command = new CommandDocument { { "saslStart", 1 }, { "mechanism", _mechanism.Name }, { "payload", currentStep.BytesToSendToServer } }; while (true) { CommandResult result; try { result = RunCommand(connection, credential.Source, command); } catch (MongoCommandException ex) { var message = "Unknown error occured during authentication."; var code = ex.CommandResult.Code; var errmsg = ex.CommandResult.ErrorMessage; if(code.HasValue && errmsg != null) { message = string.Format("Error: {0} - {1}", code, errmsg); } throw new MongoSecurityException(message, ex); } if (result.Response["done"].AsBoolean) { break; } currentStep = currentStep.Transition(conversation, result.Response["payload"].AsByteArray); command = new CommandDocument { { "saslContinue", 1 }, { "conversationId", result.Response["conversationId"].AsInt32 }, { "payload", currentStep.BytesToSendToServer } }; } } }
// public methods /// <summary> /// Authenticates the connection against the given database. /// </summary> /// <param name="connection">The connection.</param> /// <param name="credentials">The credentials.</param> public void Authenticate(MongoConnection connection, MongoCredentials credentials) { using (var conversation = new SaslConversation()) { var currentStep = _mechanism.Initialize(connection, credentials); var command = new CommandDocument { { "saslStart", 1 }, { "mechanism", _mechanism.Name }, { "payload", currentStep.BytesToSendToServer } }; while (true) { var result = connection.RunCommand(credentials.Source, QueryFlags.SlaveOk, command, true); var code = result.Response["code"].AsInt32; if (code != 0) { HandleError(result, code); } if (result.Response["done"].AsBoolean) { break; } currentStep = currentStep.Transition(conversation, result.Response["payload"].AsByteArray); command = new CommandDocument { { "saslContinue", 1 }, { "conversationId", result.Response["conversationId"].AsInt32 }, { "payload", currentStep.BytesToSendToServer } }; } } }