按属性过滤用户

时间:2013-08-26 15:11:14

标签: c# active-directory

这会从我们的ActiveDirectory中提供UserPrincipals列表,其中用户位于“x”组中:

var domainContext = new PrincipalContext(ContextType.Domain);
var groupPrincipal = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, "x");

现在,我如何通过自定义属性过滤此列表中的用户?所有用户在自定义属性“Building”中都有一个条目,我希望列表中只包含来自某个建筑物的用户。

愚蠢的我......将成员从groupPrincipal转换为DirectoryEntry,然后访问属性..

        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
            }

        }

1 个答案:

答案 0 :(得分:1)

是的,您可以使用member.GetUnderlyingObject()

var members = groupPrincipal.Members.Where(member=>(member.GetUnderlyingObject() as DirectoryEntry).Properties["building"].Value.ToString() == "NSA HQ");

Retrieve AD Custom Attribute in One Batch

中指出