Building an LDAP Web Service

Part of the You Can Learn ASP.Net and C# series.
By Ken Brown
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

Home| About Us | NewsLetters | Contact Us |

Copyright © 2004-2014 You Can Learn Series

You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home            About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for C# tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Kennos BlogBlog       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business            You Can Learn Series' Online StoreStore

You Can Learn C# and ASP.Net SeriesC# and ASP.Net
  Using Client Side Code with C# and ASP.NetClient Side Code
  Using Client Side State Management with C# and ASP.NetClient Side State Management
  Create a C# Class in Visual StudioCreate A Class
  Use A Class in C#Use A Class
  Create a Solution in Visual StudioCreate a Solution
  Create a Project in Visual StudioCreate a Project
  Create an RSS feed for your Web SiteCreate an RSS Feed
  Create an RSS feed for your Web SiteListItemCollection
  Set Events in ASP.NetSet Events ASP.Net
  Set Events in C#Set Events C#
  What is Session State and How to Use it.Session State
  Creating and Using the C# SortedList ObjectSortedList Object
  Stylesheet Tips for Visual Studio .Net and ASP.NetStyleSheet Tricks
  Using C# MethodsUsing Methods
  Pass Objects to MethodsPass Objects to Methods
  Pass Objects from MethodsPass Objects from Methods
  Using Client side State management like ViewState and Hidden fieldsView State
  LDAP, What is Lightweight Directory Access ProtocolWhat is LDAP
  Build an LDAP ReaderBuild an LDAP Reader
  Convert information from LDAP to a Web ServiceLDAP To Web Service
  Question of the dayQuestion of the Day

You Can Learn SQL SeriesSQL
You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
Kenno's BlogBlog
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Good Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Merchandise Available from You Can Learn SeriesMerchandise

Bill Welter - The Prepared Mind of a Leader

Americas Public Schools