Solved

vb.net and ldap queries

Posted on 2014-10-25
8
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 71

Accepted Solution

by:
Chris Dent earned 500 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
Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
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 …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

739 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