vb.net ldap查询try catch语句

时间:2014-10-29 16:10:49

标签: vb.net ldap

好的,我有下面的代码可以工作,它查找可能是2个域的一部分的用户。逻辑有效。我包装了一个try-catch语句来捕获不存在的空格或用户。基本上即时思考,每次用户空白或不存在时看到错误,用try-catch处理它。然后在我的try-catch中,我将搜索到的ID和一些文本添加到列表视图中。它有效,但它添加了一个空行,只有错误的ID,然后它添加了ID和我想要的文本行。不确定我的try-catch是否顺序错误?

Dim userIds As IEnumerable(Of String) = {"idthatworks", "idthatworks", "doesntwork", "idthatworks", "doesntwork"}
        For Each i As String In userIds
            Try
                Dim de As New DirectoryEntry("LDAP://domain1.net:389/DC=domain1,DC=net")
                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 = TDsearcher.FindOne()
                    Dim item As ListViewItem = ListView1.Items.Add(i)
                    item.SubItems.Add(TDresult.Properties("givenName")(0).ToString())
                    item.SubItems.Add(TDresult.Properties("cn")(0).ToString())
                    item.SubItems.Add(TDresult.Properties("userPrincipalName")(0).ToString())
                ElseIf Not res.Count <= 0 Then
                    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
            Catch ex As Exception
                Dim item As ListViewItem = ListView1.Items.Add(i)
                item.SubItems.Add("Not found in US or CA Domain")
                item.SubItems.Add("Not found in US or CA Domain")
                item.SubItems.Add("Not found in US or CA Domain")
            End Try
        Next

Heres what the listview looks like

0 个答案:

没有答案