Click here to Skip to main content
15,897,371 members
Articles / Web Development / ASP.NET
Article

Access Active Directory - The .NET Way

Rate me:
Please Sign up or sign in to vote.
2.67/5 (18 votes)
7 Jun 2007CPOL 50.3K   23   10
Accessing Active Directory

Introduction

Accessing Active Director (AD) is one of the most basic scenarios for an intranet application. For a new developer though, sometimes it becomes clumsy to get this. But believe me, it is just as simple as hitting a full toss (shows how much I love cricket).

Using the Code

The following code shows how to do it with C#. Remember to include System.DirectoryServices. Here you go:

C#
public static Hashtable SearchLDAP(string userID)
{
    DirectoryEntry entry = new DirectoryEntry("LDAP://DC=Domain,DC=com");
    DirectorySearcher mySearcher = new DirectorySearcher(entry);
    mySearcher.Filter = "(&(objectClass=user)(anr="+ userID +"))";

    mySearcher.PropertiesToLoad.Add("givenname");
    mySearcher.PropertiesToLoad.Add("sn");
    mySearcher.PropertiesToLoad.Add("mail");
    mySearcher.PropertiesToLoad.Add("description");
    mySearcher.PropertiesToLoad.Add("l");

    Hashtable associateDetailsTable = new Hashtable();
    ResultPropertyValueCollection resultCollection;
    SearchResult searchResult = mySearcher.FindOne();

    associateDetailsTable.Add("AssociateID", userID);
    if(searchResult != null)
    {
    resultCollection = searchResult.Properties["givenname"];
    foreach(object result in resultCollection)
    {
    associateDetailsTable.Add("FirstName", result.ToString());
    }
    resultCollection = searchResult.Properties["sn"];
    foreach(object result in resultCollection)
    {
    associateDetailsTable.Add("LastName", result.ToString());
    }
    resultCollection = searchResult.Properties["mail"];
    foreach(object result in resultCollection)
    {
    associateDetailsTable.Add("Mail", result.ToString());
    }
    resultCollection = searchResult.Properties["description"];
    foreach(object result in resultCollection)
    {
    associateDetailsTable.Add("Designation", result.ToString());
    }
    resultCollection = searchResult.Properties["l"];
    foreach(object result in resultCollection)
    {
    associateDetailsTable.Add("Location", result.ToString());
    }
    }
    return associateDetailsTable;
}

History

  • 7th June, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Netherlands Netherlands

Read my personal blog at www.manasbhardwaj.net.


Comments and Discussions

 
GeneralMy vote of 2 Pin
trevorde stickman18-Mar-09 21:18
trevorde stickman18-Mar-09 21:18 
GeneralMy vote of 2 Pin
tyaramis23-Dec-08 3:55
tyaramis23-Dec-08 3:55 
GeneralFilter Pin
cachobong25-May-08 21:06
cachobong25-May-08 21:06 
GeneralHashtable type Pin
cachobong21-May-08 20:40
cachobong21-May-08 20:40 
GeneralRe: Hashtable type Pin
Manas Bhardwaj21-May-08 23:46
professionalManas Bhardwaj21-May-08 23:46 
GeneralThis is no article... Pin
Dave Kreskowiak8-Jun-07 10:23
mveDave Kreskowiak8-Jun-07 10:23 
GeneralYESSSS!!!!! THANK YOU!!!! Pin
KnotBeer7-Jun-07 6:04
KnotBeer7-Jun-07 6:04 
Dude, I am waay out of the loop when it comes to programming of any sort, but my work has tasked me with creating an intranet that will eventually integrate with our SharePoint Portal Server, and AD has been one of my largest stumps!!! Everything I have found hasn't really said TO GET USER INFO FROM AD... Oh, this is my most valued article yet!!!!! THANK YOU!!!! Oh, by the way... I don't do C#, so anybody who needs a VB.NET conversion, sink your teeth into this below (This is a full-blown code-behind file to the .ASPX page):


''''''''''''''''''''''''''''''''''''''''''
''''''''Code below (contains HTML)''''''''
''''''''''''''''''''''''''''''''''''''''''
Imports System.DirectoryServices

Partial Class TryAD
Inherits System.Web.UI.Page

Protected Sub SubmitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SubmitButton.Click

Try
Dim LDAPHash As Hashtable
LDAPHash = SearchLDAP(txtboxUserLoginNameInput.Text)

For Each HashKey As DictionaryEntry In LDAPHash
Response.Write(HashKey.Value.ToString & "<br />")
Next
Response.Write("<br />That's all of it!")
Catch ex As Exception
Response.Write("ERROR: " & ex.Message)
End Try


End Sub

Public Shared Function SearchLDAP(ByVal userID As String) As Hashtable
Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://DC=ChangeToYourDomain,DC=ChangeToYourDomainSuffix")
Dim mySearcher As DirectorySearcher = New DirectorySearcher(enTry)
mySearcher.Filter = "(&(objectClass=user)(anr=" & userID & "))"

mySearcher.PropertiesToLoad.Add("givenname")
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.PropertiesToLoad.Add("mail")
mySearcher.PropertiesToLoad.Add("description")
mySearcher.PropertiesToLoad.Add("l")

Dim associateDetailsTable As Hashtable = New Hashtable()
Dim resultCollection As ResultPropertyValueCollection
Dim searchResult As SearchResult = mySearcher.FindOne()

associateDetailsTable.Add("AssociateID", userID)
If Not searchResult Is Nothing Then
resultCollection = searchResult.Properties("givenname")
Dim result As Object
For Each result In resultCollection
associateDetailsTable.Add("FirstName", result.ToString())
Next
resultCollection = searchResult.Properties("sn")
For Each result In resultCollection
associateDetailsTable.Add("LastName", result.ToString())
Next
resultCollection = searchResult.Properties("mail")
For Each result In resultCollection
associateDetailsTable.Add("Mail", result.ToString())
Next
resultCollection = searchResult.Properties("description")
For Each result In resultCollection
associateDetailsTable.Add("Designation", result.ToString())
Next
resultCollection = searchResult.Properties("l")
For Each result In resultCollection
associateDetailsTable.Add("Location", result.ToString())
Next
End If
Return associateDetailsTable
End Function
End Class

'''''''''''''''''''''''''''
'''''''''End Code''''''''''
'''''''''''''''''''''''''''


Matt Rogers

*Sigh* The kindness others may bestow upon you may be great, but those who do the most for you usually know the least that they have done anything at all.
GeneralRe: YESSSS!!!!! THANK YOU!!!! Pin
Manas Bhardwaj7-Jun-07 6:14
professionalManas Bhardwaj7-Jun-07 6:14 
GeneralRe: YESSSS!!!!! THANK YOU!!!! [modified] Pin
KnotBeer7-Jun-07 6:20
KnotBeer7-Jun-07 6:20 
GeneralRe: YESSSS!!!!! THANK YOU!!!! Pin
RaneUncle7-Jun-07 6:30
RaneUncle7-Jun-07 6:30 

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

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