vb.net 如何获取属于 Active Directory 中某个组的用户?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14710383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:12:47  来源:igfitidea点击:

How do I get the users that belong to a group in Active Directory?

vb.netactive-directoryldap

提问by Chidi Okeh

I have a dropdownlist that I am trying to fill with users that belong to a certain group in Active Directory.

我有一个下拉列表,我试图用属于 Active Directory 中某个组的用户填充该列表。

The group name is OverRiders and 8 people are members of this group. More members could be added.

群名是OverRiders,这个群有8个人。可以添加更多成员。

I have the following dropdown but I run the code, the dropdown is blank.

我有以下下拉列表,但我运行了代码,下拉列表是空白的。

What am I doing wrong?

我究竟做错了什么?

Please see code:

请看代码:

        Private Sub FillDropdown()
    Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://CN=OverRiders,OU=Departments,DC=domain,DC=com")
Dim osearcher As DirectorySearcher = New DirectorySearcher(oroot)
Dim oresult As SearchResultCollection
Dim result As SearchResult
Dim list As New List(Of String)

    osearcher.Filter = "(&(objectCategory=group)(cn={0}))"
    ' search filter; only display emp with firstname / lastname pair
    osearcher.PropertiesToLoad.Add("name") ' member
    oresult = osearcher.FindAll()


    For Each result In oresult
        If Not result.GetDirectoryEntry.Properties("name").Value Is Nothing Then
            list.Add(result.GetDirectoryEntry.Properties("name").Value.ToString())
            Call list.Sort()
        End If
Next
emplist.DataSource = list
emplist.DataBind()

End Sub

I have been able to confirm that the group does exist and the group name is valid. Thanks a lot in advance

我已经能够确认该组确实存在并且组名有效。非常感谢提前

回答by Chidi Okeh

Changed:

改变:

Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://CN=OverRiders,OU=Departments,DC=domain,DC=com")

to

Dim oroot As DirectoryEntry = New DirectoryEntry("LDAP://DC=domain,DC=com")

and this:

和这个:

osearcher.Filter = "(&(objectCategory=group)(cn={0}))"

to this:

对此:

osearcher.Filter = "(&(objectCategory=user)(memberOf=CN=overRiders,OU=Departments,DC=domain,DC=com)??)"

Everything else remain unchanged.

其他一切都保持不变。

Hope it helps someone else.

希望它可以帮助别人。

回答by Michael Gajeski

I know this is an old question, but this is what worked for me in a similar situation:

我知道这是一个老问题,但这就是在类似情况下对我有用的方法:

    Dim UsersInGroup As New Collection()

    Dim de As New DirectoryEntry("LDAP://[Domain]")

    Dim MemberSearcher As New DirectorySearcher

    With MemberSearcher
        .SearchRoot = de
        .Filter = "(&(ObjectClass=Group)(CN=" & Group & "))"
        .PropertiesToLoad.Add("Member")
    End With

    Dim mySearchResults As SearchResult = MemberSearcher.FindOne()

    For Each User In mySearchResults.Properties("Member")
        UsersInGroup.Add(User)
    Next