Solved

vb.net and ldap queries

Posted on 2014-10-25
8
221 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
This article demonstrates probably the easiest way to configure domain-wide tier isolation within Active Directory. If you do not know tier isolation read https://technet.microsoft.com/en-us/windows-server-docs/security/securing-privileged-access/s…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

635 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