Click here to Skip to main content
Licence 
First Posted 9 Feb 2002
Views 294,708
Bookmarked 74 times

Using Active Directory In ASP.NET - Dump Schema Information

By | 9 Feb 2002 | Article
An article on using System.DirectoryServices classes in ASP.NET

Sample Image - ADSI1.gif

This article is first in the series demonstrating the use of Active Directory in ASP.NET. Of course all the demo code is written in language of choice - C#. This series will not go into discussion of Active Directory or LDAP servers. We are assuming that the readers of these articles have basic understanding of these technologies.

.NET namespace and classes utilized

  • System.DirectoryServices
  • System.DirectoryServices.DirectoryEntry
  • System.DirectoryServices.DirectorySearcher
  • System.DirectoryServices.SearchResultCollection
  • System.DirectoryServices.SearchResult
  • System.DirectoryServices.ResultPropertyCollection
  • System.DirectoryServices.PropertyValueCollection

What is this article about?

Searching an Active Directory is one of the major tasks in manipulation of various resources. When I started with ADSI programming, I used to look for right kind of filter values to use. Some time I had to go back forth and look at the Active Directory schema to find value I should be using to get the information I was looking for. For example If you want to get the information when the user account was last changed, you need to create a filter looking for whenchanged property in schema. So we decided to write a small dump utility that will display all the properties that are used to describe a user's account in Active Directory.

How To Do It

The first step in using Directory Services interfaces is to make connection with the node that you want to search for. .NET framework provides DirectoryEntry class to specify the search node. For example if you want to search for a resource in whole domain, then you need to connect to the top node of domain in Active Directory. It is very important that you specify the search location as close as possible to the nearest location where the resource could be found. Otherwise the search will take longer time. For example if You want to search for a user information, you need to specify the location as User node and not the whole domain resource tree.

string strLDAP = "LDAP://pardesiservices.com"
m_obDirEntry = new DirectoryEntry(strLDAP);

After initializing the search node, you need to specify the query string in DirectorySearcher class object. You can set various parameter values of this object to fine tune your search and how the results will be returned. For this article we will only mention Filter property. This is the property that you will use to set your query string. The query string shall be specified in LDAP format. For example if you want to search for a user "foo", you can specify the query string as (cn=foo). It is very important that you specify the filter/query in parentheses. For more information on this property, look in the .NET documentation for Filter property of DirectorySearcher class.

DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
srch.Filter = "(cn=foo)";

The next step is to start the search. You will call FindAll or FindOne method on DirectorySearcher class object. If you are only interested in the first entry of the returned results, then call FindOne. Otherwise if you want to get all the search results, call FindAll method. This method returns the results as SearchResultCollection class subject.

The other property that is worth mentioning is PropertiesToLoad. This property lets you specify the values you want the search to return. If you don't specify any properties, then search returns all the properties by default. Therefore if you are only interested in some of the values, then make sure that you specify those properties in the PropertiesToLoad. This way you can avoid unnecessary loading of all the values in memory.

SearchResultCollection results;
results = srch.FindAll();

After getting all the search results, you can iterate over each SearchResult entry in the SearchResultCollection. The SearchResult class object has Properties property that returns ResultPropertyCollection object. This contains all the properties were found by search you specified.

foreach (SearchResult result in results)
{
   ResultPropertyCollection propColl = result.Properties;
}

ResultPropertyCollection exposes ProperyNames property that returns the collection containing names of all the properties returned by search. You can iterate over this collection to get the names. We used this technique to get the names of all properties exposed by User objects.

foreach (string strKey in propColl.PropertyNames)
{
  foreach (object obProp in propColl[strKey])
  {
    this.AppendPropertyNode(obTopNode, strKey, obProp);
  }
}

And then you can use this property names to extract particular values from ResultPropertyCollection dictionary.

Demo Code

We have included the demo code with this article. All the Active Directory implementation has been encapsulated in ADSIUtil class. We have also created an utility class, ADSIUser. This class parses the search results and saves as a XMLDocument. And it also exposes some properties to get specific information like First Name, Last Name, etc. This class is not complete. But we will expand this as the series progress.

Platforms Tested

We have tested the included project on following platforms
  • Windows 2000 Adv. Server
  • Windows .NET Enterprise Server (Beta 3)

Contact Us

For any suggections ot comment you can visit us as at Softomatix or write to us, softomatix@pardesiservices.com

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Softomatix

Web Developer

United States United States

Member

To learn more about us, Please visit us at http://www.netomatix.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMingtzer7:05 29 Mar '11  
GeneralA referral was returned from the server Pinmemberzeana21:45 4 Mar '08  
QuestionAD QUERYING USING LDAP NOT WORKING FROM REMOTE MACHINE PinmemberPrajin18:35 20 Jun '07  
QuestionHi guys...Connection error help with Active Directory PinmemberPhoenixzeroX9:07 23 Aug '06  
First of all i want to say hi, this is my first post in codeproject. I donwloaded the Active Directory Example and i make the changes to run this app but when i try to run the server raises a message saying "The specified domain either does not exist or could not be contacted". I have about 1 week trying to figure out why is doing that. I tried to make an example code in a windows application form but it's working fine. But when i tried to write an ASP.NET error i receive the connection error" (i guess is maybe the protocol LDAP that get's the error but i don't know why). can somebody please help me??. I write an Visual Basic NET Active directory example to check this error. In Windows Forms Works fine. In ASP.NET doesn't
 
Imports System.DirectoryServices
Dim a As New DirectoryEntry("LDAP://CN=Testfolder,OU=Users,OU=General,DC=my,DC=domain,DC=com")

Dim b As New DirectoryEntry("LDAP://CN=Phoenixzero,OU=Administrators,OU=Users,OU=Sys,OU=Sys,DC=my,DC=domain,DC=com")
Dim isMember As Boolean = Convert.ToBoolean(a.Invoke("IsMember", New Object() {b.Path}))
 
If isMember Then
MsgBox("User " & b.Properties("cn").Value & " is already a member of the group")
'
' Remove the user from the group by invoking the remove method
'
a.Invoke("Remove", New Object() {b.Path})
 
Else
'
' Add the user to the group by invoking the Add method
'
a.Invoke("Add", New Object() {b.Path})
MsgBox("User added")
End If
'
' Cleanup our allocated objects
'
If Not IsNothing(b) Then
b.Dispose()
End If
 
If Not IsNothing(a) Then
a.Dispose()
End If
 
Like i say, this code works in visual Basic .NET Windows Forms and VBScript, but doesn't work in ASP.NET webpages. I need it in webpage because is an extended option for a webpage that i will use in a webpage
 
Thanks for everything and hope you can help me
 
PhoenixzeroX
 
:confused

GeneralWorthless PinmemberUnderWing5:17 23 May '06  
GeneralFIRE BAD! ME NO LIKE! PinmemberUnderWing5:29 23 May '06  
Generalaccessing the schema itself PinmemberGiles Bradshaw6:59 1 Sep '03  
GeneralRe: accessing the schema itself PinmemberBFJoe7:23 15 May '06  
GeneralThe C# version PinmemberLDawggie9:17 20 Nov '09  
GeneralVB Code Pinsussjayommer3:34 25 Mar '03  
GeneralRe: VB Code PinmemberSlogmeister10:20 5 Mar '04  
GeneralGreat stuff and thanks! PinmemberPaul Watson22:21 10 Feb '02  
GeneralRe: Great stuff and thanks! PinsussAnonymous1:48 12 Feb '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120529.1 | Last Updated 10 Feb 2002
Article Copyright 2002 by Softomatix
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid