Reputation: 2103
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
}
}
Upvotes: 0
Views: 434
Reputation: 1463
Yes, you may use member.GetUnderlyingObject()
var members = groupPrincipal.Members.Where(member=>(member.GetUnderlyingObject() as DirectoryEntry).Properties["building"].Value.ToString() == "NSA HQ");
as pointed out in Retrieve AD Custom Attribute in One Batch
Upvotes: 1