【问题标题】:Using PrincipalContext to search LDAP (Active Directory) for all usernames, and their information使用 PrincipalContext 在 LDAP (Active Directory) 中搜索所有用户名及其信息
【发布时间】:2016-06-10 18:24:12
【问题描述】:

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

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

【问题讨论】:

    标签: vb.net ldap ldap-query principalcontext


    【解决方案1】:

    您已经可以访问搜索结果中的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
    

    【讨论】:

    • 现在测试这个,虽然在查看了你在做什么之后我看到了我的问题......不敢相信我没有看到那个。还在学习中,谢谢。几分钟后我会在测试后立即发布解决方案。
    • 感谢您的帮助!像魅力一样工作,从 10 秒变为整个广告结构的 1 秒。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多