代码之家  ›  专栏  ›  技术社区  ›  Justin Helgerson

特定Active Directory通讯组中的用户列表

  •  7
  • Justin Helgerson  · 技术社区  · 15 年前

    我正在尝试从active directory组中获取用户列表和有关该用户的一些属性。

    更新:

        Dim adGroup As New DirectoryEntry("LDAP://CN=MyGroup,OU=Groups,OU=Accounts,OU=All,DC=domain,DC=com")
        Dim adMembers As Object
        Dim objUser As ActiveDirectoryUser
        Dim objUserList As New List(Of ActiveDirectoryUser)
        Dim directoryEntry As DirectoryEntry
    
        adMembers = adGroup.Invoke("Members", Nothing)
    
        For Each adMember As Object In CType(adMembers, IEnumerable)
            directoryEntry = New DirectoryEntry(adMember)
            objUser = New ActiveDirectoryUser
    
            objUser.UserId = directoryEntry.Properties.Item("sAMAccountName").Value.ToString()
            objUser.Contract = directoryEntry.Properties.Item("ou").Value.ToString()
            objUser.LastName = directoryEntry.Properties.Item("sn").Value.ToString()
            objUser.FirstName = directoryEntry.Properties.Item("givenName").Value.ToString()
            objUser.Email = directoryEntry.Properties.Item("mail").Value.ToString()
    
            objUserList.Add(objUser)
        Next
    

    第一件工作,虽然它似乎相当低效。我的内存使用率不断攀升,因为它的执行,我得到了 this 错误,尽管看起来可以修复。第二种方法:

        Dim results As SearchResultCollection
        Dim directoryEntry2 As New DirectoryEntry("LDAP://DC=domain,DC=com")
        Dim directorySearcher As New DirectorySearcher(directoryEntry2)
        directorySearcher.PageSize = 1000
    
        directorySearcher.Filter = "(&(objectCategory=person)" & _
                               "(objectClass=user)" & _
                               "(memberOf=CN=MyGroup,OU=Groups,OU=Accounts,OU=All,DC=domain,DC=com))"
    
    
        directorySearcher.PropertiesToLoad.Add("ou")
        directorySearcher.PropertiesToLoad.Add("sn")
        directorySearcher.PropertiesToLoad.Add("givenName")
        directorySearcher.PropertiesToLoad.Add("sAMAccountName")
        directorySearcher.PropertiesToLoad.Add("mail")
    
        results = directorySearcher.FindAll
    

    结果计数似乎因应用程序的每次执行而有所不同,我觉得很奇怪。我不确定这是否是一个可靠的方式让用户回来,或者如果我需要修改我的搜索?

    2 回复  |  直到 15 年前
        1
  •  15
  •   marc_s MisterSmith    15 年前

    如果可以的话,请升级到.NET3.5并使用新的经过改进的 System.DirectoryServices.AccountManagement 命名空间。这些新课程的精彩介绍可以在 Managing Directory Security Principals in the .NET Framework 3.5 .

    这样,你的工作就变得微不足道了:

    PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "MyGroup");
    PrincipalSearchResult<Principal> members = group.GetMembers();
    

    对你有用吗?

    member 组的属性。组成员是 在层次结构中逻辑上存储为组下的子级,因此使用 DirectorySearcher

    DirectoryEntry group = new DirectoryEntry("LDAP://CN=MyGroup,OU=Groups,OU=All,DC=Domain,DC=com");
    
    foreach(object groupMemberDN in group.Properties["member"])
    {
       // grab the group member's DN
    }
    

    Quick List of C# Code Examples 对于Active Directory(或对于 Visual Basic .NET )在MSDN库中查找此代码段及更多信息。

    如果你需要 用户 属于某个特定的组(因为你想更新他们的属性或其他东西),你可以改变方法:搜索所有拥有组的用户 memberOf 与组的DN等效的属性:

     DirectoryEntry root = new DirectoryEntry("LDAP://dc=domain,dc=com");
     DirectorySearcher searcher = new DirectorySearcher(root);
    
     searcher.Filter = "(&(objectCategory=user)(memberOf=CN=MyGroup,OU=Groups,OU=All,DC=Domain,DC=com))";
     // set other properties on the searcher
    
     foreach(object result in searcher.FindAll())
     {
        // do whatever you need to do with the entry
     }
    
        2
  •  2
  •   Greg    15 年前

    扩大搜索范围,无论成员在何处:

    Dim directoryEntry As New DirectoryEntry("LDAP://OU=All,DC=Domain,DC=com")
    

    基于组成员身份筛选:

    directorySearcher.Filter = "(&(objectCategory=person)" & _
                                 "(objectClass=user)" & _
                                 "(memberOf=CN=MyGroup,OU=Groups,OU=All,DC=Domain,DC=com))"