Vb.net LDAP获取登录名列表

Vb.net LDAP获取登录名列表,vb.net,authentication,active-directory,ldap,active-directory-group,Vb.net,Authentication,Active Directory,Ldap,Active Directory Group,我需要在程序中获取组中的用户登录名列表 这是我到目前为止所拥有的,但它只返回所有的用户…我需要将这些用户减少到一个组中,其中我的名字是 Option Explicit On Imports System.DirectoryServices Imports System.DirectoryServices.ActiveDirectory Module Module1 Sub Main() Dim ADEntry As New DirectoryServices.Direc

我需要在程序中获取组中的用户登录名列表

这是我到目前为止所拥有的,但它只返回所有的用户…我需要将这些用户减少到一个组中,其中我的名字是

Option Explicit On
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory

Module Module1
    Sub Main()
        Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://OU=Users,OU=Irvine,OU=KNS,DC=corp,DC=kns,DC=com")
        Dim objSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)

        Dim oResults As DirectoryServices.SearchResultCollection
        Dim oResult As DirectoryServices.SearchResult

        '  THIS DOESNT WORK
        '  objSearch.Filter = "department = engineering"

        oResults = objSearch.FindAll

        For Each oResult In oResults
            Console.WriteLine(oResult.GetDirectoryEntry.Properties("sAMAccountName").Value)
        Next
    End Sub
End Module

尝试将文件管理器更改为

objSearch.Filter = "(&(objectCategory=user)(memberOf=CN=Employees,OU=Security Groups,DC=yourdomain,DC=com))"
该集团是一个由员工组成的集团

资料来源:


注意:我无法测试这个。让我知道它是否有效。

如果您想要组中的所有成员,请尝试以下方法:

1) 绑定到组:

DirectoryEntry theGroup = 
   new DirectoryEntry("LDAP://cn=YourGroupname,ou=SomeOU,dc=YourCompany,dc=com");
2) 然后,枚举其成员-它是组的
DirectoryEntry
的“member”属性:

foreach(object dn in theGroup.Properties["member"])
{
   Console.WriteLine(dn);
}
组的“member”属性中的每个条目都应该是其成员(用户或其他组)的完整DN(可分辨名称)

您的问题是,您试图枚举一个组的成员,但您的代码看起来更像是在枚举一个OU(组织单元)中的所有内容,这两个任务完全不同!你真正需要什么

您可以在MSDN库中找到,也可以在CodeProject(使用C#示例)中了解更多信息

马克


这成功了

几年前,我构建了一个广告组件,我们经常使用它来完成这项任务。试试这个

Public Function GetUsersInGroup(ByVal GroupName As String) As String()
        If GroupName = String.Empty Then Return Nothing
        Dim Users() As String = Nothing
        Dim S As String = "LDAP://DC=YourCompany,DC=com"
        Dim Parent As New DirectoryServices.DirectoryEntry(S)
        Dim Search As New DirectoryServices.DirectorySearcher(Parent)

        Search.SearchScope = DirectoryServices.SearchScope.Subtree
        Search.Filter = "(CN=" & GroupName & ")"
        Search.PropertiesToLoad.Add("member")

        Dim Result As DirectoryServices.SearchResult = Search.FindOne
        Dim prop_value As String, i As Integer = 0
        If Result IsNot Nothing Then
            If Result.Properties("member").Count > 0 Then
                ReDim Users(Result.Properties("member").Count - 1)
                For Each prop_value In Result.Properties("member")
                    Dim S2 As New DirectoryServices.DirectorySearcher(Parent)
                    S2.SearchScope = DirectoryServices.SearchScope.Subtree
                    S2.Filter = "(" & prop_value.Substring(0, prop_value.IndexOf(","c)) & ")"
                    S2.PropertiesToLoad.Add("SAMAccountName")
                    Dim R2 As DirectoryServices.SearchResult = S2.FindOne
                    For Each Prop As String In R2.Properties("SAMAccountName")
                        Users(i) = Prop.ToUpper
                        i = i + 1
                    Next
                Next
                Exit For
            End If
        End If
End Function
如果你知道去哪里找的话,你可以从广告中获得很多信息

Public Function GetUsersInGroup(ByVal GroupName As String) As String()
        If GroupName = String.Empty Then Return Nothing
        Dim Users() As String = Nothing
        Dim S As String = "LDAP://DC=YourCompany,DC=com"
        Dim Parent As New DirectoryServices.DirectoryEntry(S)
        Dim Search As New DirectoryServices.DirectorySearcher(Parent)

        Search.SearchScope = DirectoryServices.SearchScope.Subtree
        Search.Filter = "(CN=" & GroupName & ")"
        Search.PropertiesToLoad.Add("member")

        Dim Result As DirectoryServices.SearchResult = Search.FindOne
        Dim prop_value As String, i As Integer = 0
        If Result IsNot Nothing Then
            If Result.Properties("member").Count > 0 Then
                ReDim Users(Result.Properties("member").Count - 1)
                For Each prop_value In Result.Properties("member")
                    Dim S2 As New DirectoryServices.DirectorySearcher(Parent)
                    S2.SearchScope = DirectoryServices.SearchScope.Subtree
                    S2.Filter = "(" & prop_value.Substring(0, prop_value.IndexOf(","c)) & ")"
                    S2.PropertiesToLoad.Add("SAMAccountName")
                    Dim R2 As DirectoryServices.SearchResult = S2.FindOne
                    For Each Prop As String In R2.Properties("SAMAccountName")
                        Users(i) = Prop.ToUpper
                        i = i + 1
                    Next
                Next
                Exit For
            End If
        End If
End Function