使用 PrincipalContext 在 LDAP (Active Directory) 中搜索所有用户名及其信息

Using PrincipalContext to search LDAP (Active Directory) for all usernames, and their information

我在使用这段代码时遇到问题...显然,它可以工作,但对我来说太慢了,有人有什么想法吗?当我尝试访问以下内容时速度变慢

Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, p.SamAccountName)

同样,下面的代码工作得很好,但速度很慢。如果我取出上面的一段代码并搜索 p.SamAccountName 它会在 1 秒内完成,所以我确定我做错了什么。

Dim sw As New Stopwatch

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    sw.Start()

    DataGridView1.ColumnCount = 3
    DataGridView1.Columns(0).Name = "Account Name"
    DataGridView1.Columns(1).Name = "First Name"
    DataGridView1.Columns(2).Name = "Last Name"

    Dim ctx = New PrincipalContext(ContextType.Domain, "JOI", DomainName)
    Dim userPrin As New UserPrincipal(ctx)
    userPrin.Name = "*"
    Dim searcher = New System.DirectoryServices.AccountManagement.PrincipalSearcher()
    searcher.QueryFilter = userPrin
    Dim results = searcher.FindAll()

    For Each p As Principal In results
        Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, p.SamAccountName)
        Dim row As String() = New String() {u.SamAccountName, u.GivenName, u.Surname}
        DataGridView1.Rows.Add(row)
    Next

    sw.Stop()
    MessageBox.Show("Finished in :" & sw.Elapsed.Duration.Seconds & " seconds")

End Sub

Private Shared Function DomainName() As String

    Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
    DomainName = objRootDSE.Properties("defaultNamingContext")(0)

End Function

您已经可以访问搜索结果中的 UserPrincipal,然后您正在对结果中的每个项目执行另一次新搜索。这正是减慢您的功能的原因。

我通常使用过滤器 (LINQ) 进行过滤,并且只获取 UserPrincipal 类型的结果项。这样它只是一次通过搜索

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    sw.Start()

    DataGridView1.ColumnCount = 3
    DataGridView1.Columns(0).Name = "Account Name"
    DataGridView1.Columns(1).Name = "First Name"
    DataGridView1.Columns(2).Name = "Last Name"

    Using context As PrincipalContext = New PrincipalContext(ContextType.Domain, "JOI", DomainName)
        Using userPrin As UserPrincipal = New UserPrincipal(context)
            userPrin.Name = "*"
            Using searcher As PrincipalSearcher = New PrincipalSearcher(userPrin)

                Dim results = searcher _
                    .FindAll() _
                    .OfType(Of UserPrincipal)()

                For Each p As UserPrincipal In results
                    Dim row As String() = New String() {p.SamAccountName, p.GivenName, p.Surname}
                    DataGridView1.Rows.Add(row)
                Next
            End Using
        End Using
    End Using

    sw.Stop()
    MessageBox.Show("Finished in :" & sw.Elapsed.Duration.Seconds & " seconds")

End Sub