Reputation: 157
I am attempting to write an LDAP query via VB.NET to pull back the atrribute of homeDirectory and throw in in a textbox. The below code is used to query an LDAP user to check a group theyre a part of. How can i turn the below in to pull back homeDirectory = textbox1.text.
Private Sub Button3_Click(sender As Object, e As System.EventArgs) Handles Button3.Click
Dim rootEntry As New DirectoryEntry("fake")
Dim srch As New DirectorySearcher(rootEntry)
srch.SearchScope = SearchScope.Subtree
srch.Filter = "(&(objectClass=user)(sAMAccountName=fake)(memberOf=CN=FAKE,OU=Citrix,OU=SecurityGroups,DC=fake,DC=fake))"
Dim res As SearchResultCollection = srch.FindAll()
If res Is Nothing OrElse res.Count <= 0 Then
MsgBox("This user is *NOT* member of that group")
Else
MsgBox("This user is INDEED a member of that group")
End If
End Sub
What it looks like now:
Dim rootEntry As New DirectoryEntry("LDAP://johndoe")
Dim srch As New DirectorySearcher(rootEntry)
srch.SearchScope = SearchScope.Subtree
srch.Filter = "(&(objectClass=user)(sAMAccountName=)"
srch.PropertiesToLoad.Add("homeDirectory")
Dim res As SearchResultCollection = srch.FindAll()
Dim homeDirectory As String = res(0).Properties("homeDirectory").ToString()
Dim user As SearchResult = res(0)
If user.Properties.Contains("homeDirectory") Then
TextBox1.Text = user.Properties("homeDirectory").ToString()
End If
If res Is Nothing OrElse res.Count <= 0 Then
MsgBox("This user is *NOT* member of that group")
Else
MsgBox("This user is INDEED a member of that group")
End If
Upvotes: 0
Views: 3466
Reputation: 107586
Tell the DirectorySearcher
which attributes you want to load (e.g.):
srch.PropertiesToLoad.Add("homeDirectory")
You can probably simplify the query now to just include the username for which you're looking:
srch.Filter = "(&(objectClass=user)(sAMAccountName=fake))"
Then, assuming you have just one search result, you should be able to retrieve that property from the first result in your SearchResultCollection
:
Dim user As SearchResult = res(0)
If user.Properties.Contains("homeDirectory") Then
textbox1.Text = user.Properties("homeDirectory").ToString()
End If
Upvotes: 0