Filter users by attribute
This gives a list of UserPrincipals from our ActiveDirectory where Users
are in group "x":
var domainContext = new PrincipalContext(ContextType.Domain);
var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext,
IdentityType.SamAccountName, "x");
Now how would I filter the users in this list by a custom attribute? All
users have an entry in the custom property "Building", and I want to the
list to contain only users from a certain building.
SOLUTION
stupid me ... cast the members from groupPrincipal to DirectoryEntry, then
access properties ..
foreach (var member in groupPrincipal.Members)
{
// maybe some try-catch ..
System.DirectoryServices.DirectoryEntry i =
(System.DirectoryServices.DirectoryEntry)member.GetUnderlyingObject();
if (i.Properties["building"].Value.toString() == "NSA HQ")
{
// Do stuff here
}
}
No comments:
Post a Comment