Reading Information from the LDAP Server

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

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

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

Camping and Hiking supplies


Americas Public Schools

Learn insider tips from one of the web’s biggest podcasters Pat Flynn!

Tap Influence Co-Founder Holly Hamann details how to make your blog more brand friendly.

Tiny Cupcake Toppers, with Sheryl Bito

Figure Anatomy for the Artist, with Roberto Osti

BODYBUILDING PROTEIN

Shop Seattle Seahawks Super Bowl Championship Gear at Fanatics.com

Dog Supplies

Lowest Prices on Fruit Trees, Shade Trees, Flower Bulbs, Perennials, House plants, Vegetable Plants, Seed and More!