derek7467
asked on
vb.net 2 domain ldap query
I have a program i developed that looks up user info in LDAP and returns it to a listview. It works fine with one domain, when i try to include the second in an IF statement it fails like something is empty in LDAP, which is not blank when i manually check. The logic in my if statement is probably flawed, can someone take a peek?
Basically, if it cant find the userid in the first search, it should look again in the second domain, and return the results. Also, how can i turn this into an ELSEIF statement? I would like to have a third else statement that says if the ids arent found in either domain then "do something".
Thanks!
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
Basically, if it cant find the userid in the first search, it should look again in the second domain, and return the results. Also, how can i turn this into an ELSEIF statement? I would like to have a third else statement that says if the ids arent found in either domain then "do something".
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you add it as a filter or property?
-saige-
Imports System.DirectoryServices
Imports System.Text
Module Module1
Sub Main()
Dim userIds As IEnumerable(Of String) = {"test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"}
Dim de As DirectoryEntry
Dim filter As String
Dim searcher As DirectorySearcher
Dim result As SearchResult
Dim results As SearchResultCollection
Dim users As New Dictionary(Of String, Dictionary(Of String, ResultPropertyValueCollection))
Dim tempUser As Dictionary(Of String, ResultPropertyValueCollection)
Dim write As New StringBuilder
For Each i As String In userIds
tempUser = New Dictionary(Of String, ResultPropertyValueCollection)
de = New DirectoryEntry("LDAP://DC01/DC=domain1,DC=com")
filter = String.Format("(sAMAccountName={0})", i)
searcher = New DirectorySearcher(de, filter)
result = searcher.FindOne()
results = searcher.FindAll()
If results Is Nothing OrElse results.Count <= 0 Then
de = New DirectoryEntry("LDAP://DC02/OU=Users,DC=domain2,DC=com")
filter = String.Format("(sAMAccountName={0})", i)
searcher = New DirectorySearcher(de, filter)
result = searcher.FindOne()
If Not result Is Nothing Then
tempUser.Add("Dispaly Name", result.Properties("displayName"))
tempUser.Add("Departments", result.Properties("departmentNumber"))
tempUser.Add("Title", result.Properties("title"))
tempUser.Add("Principal Name", result.Properties("userPrincipalName"))
Else
tempUser.Add("Dispaly Name", Nothing)
tempUser.Add("Departments", Nothing)
tempUser.Add("Title", Nothing)
tempUser.Add("Principal Name", Nothing)
End If
ElseIf Not results Is Nothing AndAlso results.Count > 0 Then
'' Other statements here
tempUser.Add("Dispaly Name", result.Properties("displayName"))
tempUser.Add("Departments", result.Properties("departmentNumber"))
tempUser.Add("Title", result.Properties("title"))
tempUser.Add("Principal Name", result.Properties("userPrincipalName"))
Else
tempUser.Add("Dispaly Name", result.Properties("displayName"))
tempUser.Add("Departments", result.Properties("departmentNumber"))
tempUser.Add("Title", result.Properties("title"))
tempUser.Add("Principal Name", result.Properties("userPrincipalName"))
End If
users.Add(i, tempUser)
Next
For Each user As KeyValuePair(Of String, Dictionary(Of String, ResultPropertyValueCollection)) In users
write.AppendLine("================================================================================")
write.AppendLine(String.Format("User: {0}", user.Key))
For Each [Property] As KeyValuePair(Of String, ResultPropertyValueCollection) In user.Value
write.Append(String.Format("{0}: ", [Property].Key))
If Not [Property].Value Is Nothing Then
Dim firstValue As Boolean = True
For Each value As Object In [Property].Value
If firstValue Then
write.Append(value)
firstValue = False
Else
write.Append(String.Format(", {0}", value))
End If
Next
Else
write.Append("No value defined for this property.")
End If
write.AppendLine()
Next
write.AppendLine("================================================================================")
write.AppendLine()
Next
Console.WriteLine(write)
Console.ReadLine()
End Sub
End Module
-saige-
ASKER
I have another question, i changed my search criteria to specify departmentNumber and it only brings back one entry when i know there should be around 100, any reason you think im only receiving one result?