How to Query LDAP and display your domain's Global Address List using VB.NET





0/5 (0 vote)
A quick run through of how to quickly query LDAP and how to use the resutls of that query.
Introduction
A short while ago I wanted to quickly query LDAP to throw together a simple rendering of my domains Global Address List for non-domain users. Unlike standard T-SQL and Oracle datasources, I found that there was not a lot of information that showed a simple way to query the data and once I had it I needed a simple way to display it and binding it to a traditional Gridview seemed out of the questions given the unconventional source of this data. What I aim to do with this article is give you a simple page load event that you can place into a blank asp template and immediately see results. Of course this is not just a code sample, I want to explain what each portion does so that you can customize it to your needs as well.Before you begin
This article assumes that you are running the ASP content from a PC/Server via IIS that is a member of a domain. We are not going to specify credentials or FQDN so these values will be retrieved using your current credentials and systems domain membership. Also on your code behind if when you import "System.DirectoryServices" if you have issues you may need to add a web reference to it, which is easy because it should be listed on the Net tab when you select Add Reference from your project.Let's get started
- Add a simple header row to your HTML table. This is done with the
Response.Write
command as is the rest of the table that we are going to create. So, in yourPage_Load
Event for your code behind, paste the following line to write the basic HTML table header row: - Next declare a variable that will be used for referencing the
DirectorySearcher
class properties in theSystem.DirectoryServices
namespace. - Next declare a string and fill it with the
SearchRoot
Path property found in theDirectorySearcher
class. - Now using the
objsearch
variable we created earlier we will need to set some properties and values before we initiate the search of AD. This block filters the search to user objects, defines the scope of the search and specifies the return of “common name” object properties in the search. - Next with this block we specify that we want to retrieve only the names of attributes that have assigned values as well as define a sort property and direction.
- Now we define a collection variable of type
SearchResultCollection
and call theFindAll
method of theDirectorySearcher
class. This of course executes the query and places the results into this variable. - Finally using a For Each loop we will run through the items in the collection and use the
Response.Write
method to write the contents of each row into our HTML table. - Lastly we close out the process by writing the closing tag to the HTML table.
Response.Write("<table border=1 bordercolor=#000000><tr> " & _
"<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
"<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
Dim objsearch As DirectorySearcher = New DirectorySearcher
Dim strrootdse As String = objsearch.SearchRoot.Path
See the object reference link at the bottom for more info on AD object properties
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))"
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
objsearch.PropertiesToLoad.Add("cn")
objsearch.PropertyNamesOnly = True
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
objsearch.Sort.PropertyName = "cn"
Dim colresults As SearchResultCollection = objsearch.FindAll
For Each objresult As SearchResult In colresults
Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
"</td><td><a href='mailto:" & objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
objresult.GetDirectoryEntry.Properties("mail").Value & "</a></td></tr>")
Next
Response.Write("</table>")
Because this is all in the Page Load Event simply access the page in IIS or start debugging in VS/VWD and you should be presented with a quick and easy listing of AD properties.
End Result
My hope is that you can see how you can quickly incorporate this into your own project whether it involves validating a domain account or displaying domain member data. Here is the completed page load event.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Response.Write("<table border=1 bordercolor=#000000><tr> " & _
"<td bgcolor=#ffff99 width=190px align=center>Employee Name</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Phone</td>" & _
"<td bgcolor=#ffff99 width=150px align=center>Mobile</td>" & _
"<td bgcolor=#ffff99 width=250px align=center>E-mail Address</td></tr>")
Dim objsearch As DirectorySearcher = New DirectorySearcher
Dim strrootdse As String = objsearch.SearchRoot.Path
objsearch.Filter = "(& (mailnickname=*)(objectClass=user))"
objsearch.SearchScope = System.DirectoryServices.SearchScope.Subtree
objsearch.PropertiesToLoad.Add("cn")
objsearch.PropertyNamesOnly = True
objsearch.Sort.Direction = System.DirectoryServices.SortDirection.Ascending
objsearch.Sort.PropertyName = "cn"
Dim colresults As SearchResultCollection = objsearch.FindAll
For Each objresult As SearchResult In colresults
Response.Write("<tr><td>" & objresult.GetDirectoryEntry.Properties("cn").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("telephoneNumber").Value & _
"</td><td>" & objresult.GetDirectoryEntry.Properties("mobile").Value & _
"</td><td><a href='mailto:" & _
objresult.GetDirectoryEntry.Properties("mail").Value & "'>" & _
objresult.GetDirectoryEntry.Properties("mail").Value & _
"</a></td></tr>")
Next
Response.Write("</table>")
End Sub