I'm trying to authenticate users against Active Directory and I'm using the code below to validate their credentials.
bool isValid = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);
if (isValid)
{
userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
}
My problem is that the ValidateCredentials method doesn't validate the user's password when username = "domain\username"
and always returns true, but when the username = "username"
or [email protected]
, it works and return false when the password is invalid.
Scenario 1:
username = "CorrectUserName" and password = "IncorrectPassword" => isValid = false.
username = "CorrectUserName" and password = "CorrectPassword" => isValid = true.
Scenario 2:
username = "[email protected]" and password = "IncorrectPassword" => isValid = false.
username = "[email protected]" and password = "CorrectPassword" => isValid = true.
Scenario 3 (this is my problem):
username = "Domain\CorrectUserName" and password = "IncorrectPassword" => isValid = true.
username = "Domain\CorrectUserName" and password = "CorrectPassword" => isValid = true.
My code looks like this tutorial with minor changes.
I don't know what I'm doing wrong here.
false
for correct and incorrect passwords as I'd expect. – itsme86