Part of the
You Can Learn ASP.Net and C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
Updated: October 26, 2004
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
Okay, now we are ready to create our web service and web methods that call this
class and method. Open the code behind on the LDAPWebServ.asmx file. Create a
new WebMethod.
As you can see, if you create your class correctly, there isn't much to
creating the WebMethod. First we create the filter we want to use. The method
we are creating is GetAllUsers. We want to bring back all users on the
Directory Server. The filter needed for this step will be dependent on your
specific LDAP object. For the Sun One LDAP I used "uid=*". But as you can see,
the Active Directory required the filter "samaccountname=*". You have to see
your data and then determine at what level to filter.
Then we create an instance of the LDAPComLink object, the class we built
earlier. Create an instance of an XmlDocument and then call our GetLDAPInfo
method, pass in the filter information and capture the XmlDocument object that
is returned. Then the method itself returns the XmlDocument to the caller.
If you don't do anything more than what we have gotten up to this point you
should be able to call your method and return the user information from the
LDAP server. Set LDAPWebServ.asmx as start page and then run your web service.
Choose the link "GetAllUsers" and it should flash up a page with an Invoke
button. Click the button and if we did everything right we should see our XML
of users and other properties returned from the LDAP application.
To make this web service more robust, you can send the configuration and
connectivity information with the filter to the class. You can also add a
method for authenticating a user and another method for returning all the
groups. All this information is available in the LDAP server, you just need to
know how to query it for the right information.
In the code that is provided with this article, I have added additional queries
you can send to the Directory server. Hopefully, you will find them useful.
When accessing an LDAP directory server, you need to build a DirectoryEntry and
DirectorySearcher objects. Place the results in a SearchResultCollection and
then filter on the Collection. To pick out individual pieces from the LDAP,
just iterate through the collection pulling out properties and their values.
If you place all the work of querying the LDAP server and the connectivity
issues in another class, then the WebMethods from the WebService are simple to
create and clutter free. If you have multiple LDAP servers to access, then
create a configuration file and get the connectivity information from the
configuration file.
This article presented how to access an LDAP directory, the C#.Net way to read
the information in the LDAP server and then how to use XmlDocument objects to
present them to a Web Service. And finally, how to create the Web Service using
the [WebMethod] keyword.
Happy Programming.
Go To Page 1 /
2 / 3
|