Solved

vb.net and ldap queries

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

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 70

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
 

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 70

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

Join & Write a Comment

[b]Ok so now I will show you how to add a user name to the description at login. [/b] First connect to your DC (Domain Controller / Active Directory Server) SET PERMISSIONS FOR SCRIPT TO UPDATE COMPUTER DESCRIPTION TO USERNAME 1. Open Active …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now