[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

vb.net and ldap queries

Greetings!  I am looking for a way to query LDAP from vb.net with a textfile list of usernames and pull back attributes from that list in ldap.  For instance, I have a list of 20 users and I would like to pull back their email address in ldap.  Best scenario for me is I load the list to a listbox, click a query button and return both the ID and the email in another listbox.  Any ideas?  I have googled this and searched this site and haven't come up with much.
0
derek7467
Asked:
derek7467
  • 5
  • 3
1 Solution
 
Chris DentPowerShell DeveloperCommented:
You could read the file into an ArrayList using System.IO.FileStream with StreamReader (which exposes a ReadLine method). Populate the drop down list from that on load or on file selection.

The best way to implement the searcher component goes something like this (my VB.NET is very rusty):
Dim de as new DirectoryEntry()
Dim LdapFilter as String = "(sAMAccountName=" & DropDownList.Text & ")"
Dim searcher as New DirectorySearcher(de, DropDownList.Text)
Dim result as SearchResult = searcher.FindOne()
EmailBox.Text = result.Properties("mail")(0).ToString()

Open in new window

Tie that to your button click and it "should" work. I suspect you'll need to pay some attention to error handling, especially for searches that fail.

Chris
0
 
derek7467Author Commented:
Thanks chris. Emailbox.text could theoretically be list view that adds all returned values there?
0
 
Chris DentPowerShell DeveloperCommented:
Yes, the searcher component will return the e-mail address for any account you feed into it. There's not a whole lot you can do to optimise it further if you have a random selection of accounts you want that for.

Chris
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
derek7467Author Commented:
ok ill try this on Tuesday when I get back into the office (didn't bring my laptop home, doh..)
0
 
derek7467Author Commented:
So i rewrote your code to the below and it works when i select a userID in the listbox.  How do i make this loop through each record in the listbox and pull back the display name and add it to the listbox2 i have listed below?

       Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
        Dim LdapFilter As String = "(sAMAccountName=" & ListBox1.Text & ")"
        Dim searcher As New DirectorySearcher(de, LdapFilter)
        Dim result As SearchResult = searcher.FindOne()
        ListBox2.Items.Add(result.Properties("displayName")(0).ToString())

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:
Perhaps make that section into a method? Then all you need is a foreach loop and a call to that method?

Chris
0
 
derek7467Author Commented:
Im not that great with For Each, can you help?  I tried the below and cant get it to loop through each listbox entry:

 
 Dim de As New DirectoryEntry("LDAP://test.com:389/DC=test,DC=com")
        Dim LdapFilter As String = "(sAMAccountName=" & ListBox1.Text & ")"
        Dim searcher As New DirectorySearcher(de, LdapFilter)
        Dim result As SearchResult = searcher.FindOne()
        ListBox2.Items.Add(result.Properties("displayName")(0).ToString())
        For i As Integer = 0 To ListBox1.Items.Count - 1
            Dim ADEntry As DirectoryEntry = New DirectoryEntry(result.Path)
            If result.Properties("displayName") Is Nothing Then
                Return
            End If
        Next

Open in new window



Im assuming ihave to change the .text in this string:
Dim LdapFilter As String = "(sAMAccountName=" & ListBox1.Text & ")"
to something like:

Dim LdapFilter As String = "(sAMAccountName=" & ListBox1.items & ")"
That doesnt work, it underlines and says i cant use that with an "&"
0
 
derek7467Author Commented:
got it:

   For Each i As String In ListBox1.Items
            Dim de As New DirectoryEntry("LDAP://test.com/DC=test,DC=com")
            Dim LdapFilter As String = "(sAMAccountName=" & i & ")"
            Dim searcher As New DirectorySearcher(de, LdapFilter)
            Dim result As SearchResult = searcher.FindOne()
            ListBox2.Items.Add(result.Properties("displayName")(0).ToString())
            ListBox2.Items.Add(result.Properties("title")(0).ToString())
            Dim ADEntry As DirectoryEntry = New DirectoryEntry(result.Path)
            If result.Properties("displayName") Is Nothing Then
                On Error Resume Next
            End If
        Next

Open in new window

0

Featured Post

Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now