I'm developing a C# .NET Framework library to access active directory.
One of the things that I have to do is to get all AD users, and I see that:
PrincipalContext principalContext =
new PrincipalContext(ContextType.Domain,
domainName.Trim(),
domainContainer.Trim());
And
PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
Returns the same users with this code:
// define a "query-by-example" principal - here, we search for all users
UserPrincipal qbeUser = new UserPrincipal(principalContext);
// create your principal searcher passing in the QBE principal
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var found in srch.FindAll())
{
UserPrincipal user = found as UserPrincipal;
if (user != null)
{
Console.WriteLine(user.SamAccountName);
}
}
When do I need to use a Domain Name and a Domain Container?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…