简体   繁体   中英

PrincipalSearcher Wrong Username Or Password error

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?

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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