I'm using a custom UserPrincipalEx-class since I want to query the Active Directory for a custom attribute "costCenter". The custom UserPrincipalEx-Class therefore has a DirectoryProperty("costCenter") added. The basic and long-running (3 minutes) version looks like this:
// create domain context
PrincipalContext context = new PrincipalContext(ContextType.Domain, "myDomainController");
// define the query-by-example principal
UserPrincipal qbeUser = new UserPrincipal(context);
// create the principal searcher
PrincipalSearcher searcher = new PrincipalSearcher(qbeUser);
// find all matches
foreach (var hit in searcher.FindAll())
{
//do incredible stuff here
}
Now, using a customized class "UserPrincipalEx" that is extended for the custom attribute "costCenter":
UserPrincipalEx qbeUser = new UserPrincipalEx(context);
qbeUser.costCenter = "123";
makes the query execute almost as fast as light. But what I really need would be to query for all users that just have a costCenter (not all do so).
So my question is: How to use an extended "Query-By-Example" principal to search for principals that just have a custom attribute?