vb.net ldap同时查询2个域名

时间:2014-10-29 13:14:03

标签: vb.net ldap

我开发了一个程序,用于在LDAP中查找用户信息并将其返回到列表视图。它适用于一个域,当我尝试在IF语句中包含第二个时,它失败就像在LDAP中的某些东西是空的,当我手动检查时它不是空白。我的if语句中的逻辑可能存在缺陷,有人可以看一眼吗?

 Dim userIds As IEnumerable(Of String) = {"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"}
        For Each i As String In userids
            Dim de As New DirectoryEntry("LDAP://domain1.com:389/DC=domain1,DC=com")
            Dim LdapFilter As String = "(sAMAccountName=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult = searcher.FindOne()
            Dim res As SearchResultCollection = searcher.FindAll()

            If res Is Nothing OrElse res.Count <= 0 Then
                Dim tdbfg As New DirectoryEntry("LDAP://domain2.com:389/OU=Users,OU=domain2,DC=domain2,DC=com")
                Dim TDLdapFilter As String = "(sAMAccountName=" & i & ")"
                Dim TDsearcher As New DirectorySearcher(tdbfg, TDLdapFilter)
                Dim TDresult As SearchResult = searcher.FindOne()
                Dim item As ListViewItem = ListView1.Items.Add(i)
                item.SubItems.Add(result.Properties("displayName")(0).ToString())
                item.SubItems.Add(result.Properties("title")(0).ToString())
                item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
            Else
                Dim item As ListViewItem = ListView1.Items.Add(i)
                item.SubItems.Add(result.Properties("displayName")(0).ToString())
                item.SubItems.Add(result.Properties("title")(0).ToString())
                item.SubItems.Add(result.Properties("userPrincipalName")(0).ToString())
            End If
        Next

基本上,如果它在第一次搜索中找不到用户ID,它应该再次在第二个域中查找,并返回结果。另外,我如何将其转换为ELSEIF语句?我想有第三个其他声明,说明如果在任一域中找不到ids,那么“做某事”。

谢谢!

1 个答案:

答案 0 :(得分:0)

啊,我的变量在其他部分错了!

希望这有助于其他人。