PrincipalSearcher Wrong Username Or Password error

1.6k views Asked by At

I'm trying to get user data from the active directory. Authentication process is true.

var context = new PrincipalContext(ContextType.Domain, "localhost-ad.local", "OU=LocalOu,DC=localhost-ad,DC=local", ContextOptions.Negotiate);
var login = context.ValidateCredentials("user.name", "password", ContextOptions.Negotiate);

But PrincipalSearcher returns a wrong username or password.

var user = new UserPrincipal(context);

var searcher = new PrincipalSearcher(user);
var searchResults = searcher.FindAll();

List<UserPrincipal> results = new List<UserPrincipal>();

foreach (Principal p in searchResults)
{
    results.Add(p as UserPrincipal);
}

return results;

How can I solve this problem?

1

There are 1 answers

0
Ashigore On BEST ANSWER

I think your problem may stem from a misunderstanding of what PrincipalContext.ValidateCredentials does.

It does not authenticate the context on the domain or allow access to the domain in any way, all it does is check if the username and password are correct and return True or False.

Instead you can authenticate with the domain as so:

var context = new PrincipalContext(ContextType.Domain, "localhost-ad.local",
    "OU=LocalOu,DC=localhost-ad,DC=local", "user.name", "password");