Download the Source Code
This is a three part article on building an LDAP web service.
LDAP, What is Lightweight Directory Access Protocol
Reading Information from the LDAP Server
Converting LDAP to Web Service
So far we have told you what an LDAP server is and now we will show you how to
connect to it. Bring information down from the server and format it into an
XmlDocument object. So let's add some variables to our method. I have
initialized them to string.Empty and for the instance of AuthenticationTypes, I
have initialized it to Anonymous.
Next we populate the connectivity information with real values:
Yes, we could have populated this information when we initialized the objects,
but if you want to pass in multiple Directory servers, Active Directory, Sun
One, etc., then this allows you to create the variable and then populate it
later after user input.
Then we create an instance of a DirectoryEntry object. This object encapsulates
a node in the Directory Services hierarchy. We pass into the object the
connectivity information we created previously.
To actually query the Directory server, we will create an instance of a
DirectorySearcher object and we will pass into the DirectorySearcher object the
DirectoryEntry object we created in the previous step.
The results of the query are stored in a collection object,
SearchResultCollection, called results.
We still want to filter the final results so we add the filter query to the
mySearcher object.
And finally, we query the LDAP repository, storing the results in the results
collection.
The rest of the formula is simple yet slightly confusing in its implementation.
We have all the information returned for our query of the Directory Server
stored in a SearchResultCollection object as a collection of properties and
values. We are going to build our own XML object to return to our calling web
service. Using a foreach loop, we iterate through the result collection,
pulling out properties first and then within each property we find the actual
value stored in the hierarchy. The code below has been shortened to show how
the loop through the collection works.
The "key" that we need is determined by the LDAP directory you are using. Sun
One uses different keys than Microsoft's Active Directory. So the system
administrator might be able to inform you of the keys to use for your specific
application, or you filter at a higher level and see what keys are returned and
break it down yourself.
Each user that we select from our query we are going to wrap in a <User>
tag of our XML object. Notice that we create them as a StringBuilder object and
then before we send them to the calling object, we load them into an
XmlDocument object. We could have created an XML schema and then loaded them
into the XmlDocument as we go, but for this example, I felt that was more
information than needed.
This page presented the C# code behind needed to read information from an LDAP
directory server. Next page, we format the WebService and present the
information gleaned from the LDAP server as a [WebMethod].
Go To Page 1 /
2 / 3