Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net and ldap queries

Posted on 2014-10-25
8
Medium Priority
?
224 Views
Last Modified: 2014-10-28
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
Comment
Question by:derek7467
  • 5
  • 3
8 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 40404538
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
 

Author Comment

by:derek7467
ID: 40404758
Thanks chris. Emailbox.text could theoretically be list view that adds all returned values there?
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40404910
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:derek7467
ID: 40404967
ok ill try this on Tuesday when I get back into the office (didn't bring my laptop home, doh..)
0
 

Author Comment

by:derek7467
ID: 40408524
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
 
LVL 71

Expert Comment

by:Chris Dent
ID: 40408548
Perhaps make that section into a method? Then all you need is a foreach loop and a call to that method?

Chris
0
 

Author Comment

by:derek7467
ID: 40408643
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
 

Author Comment

by:derek7467
ID: 40409055
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question