Introduction
Last month the project manager asked me write to find all users information from the Active directory and which all fields are missing information for particular user. I was trying to search in Internet for information about .NET Active Directory examples, I could not able to find much information on the net, it prompted me write an article on this topic.
In this article, I will explain how to use Active Directory class and retrieve data from the component classes. You can cut and past below code lines and execute it but you need to pass domain name in Directory Entry constructor. Following example taken from one of my developed projects and modified for easy to understand.
I assumed that you have a general understanding of active directory before using this example.
Step 1:
Add System.DirectoryServices.Dll (from Project Add reference)
System.DirectoryServices
provides easy access to active directory from managed code. This namespace contains two components classes, DirectoryEntry
and DirectorySearcher
.
Step 2:
Using System.DirectoryServices
Directory Entry Class: this class encapsulates a node or object in the active directory hierarchy. Use this class for binding to objects, reading properties and updating attributes.
Step 3:
DirectoryEntry entry = new DirectoryEntry("LDAP://DomainName");
Directory Searcher: It will perform queries against the active directory hierarchy
Step 4:
DirectorySearcher Dsearch = new DirectorySearcher(entry);
Step 5:
String Name="Richmond";
The Filter
property supports for the all filter the information of the active directory.
Step 6:
dSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
Executes the search and returns a collection of the entries that are found.
Step 7:
This function checks active directory field is valid or not. Add this member function to you class.
Public static string GetProperty(SearchResult searchResult,
string PropertyName)
{
if(searchResult.Properties.Contains(PropertyName))
{
return searchResult.Properties[PropertyName][0].ToString() ;
}
else
{
return string.Empty;
}
}
Step 8:
foreach(SearchResult sResultSet in dSearch.FindAll())
{
Console.WriteLine(GetProperty(sResultSet,"cn"));
Console.WriteLine(GetProperty(sResultSet,"givenName"));
Console.Write(GetProperty(sResultSet,"initials"));
Console.Write(GetProperty(sResultSet,"sn"));
string tempAddress=GetProperty(sResultSet,"homePostalAddress");
if(tempAddress !=string.Empty)
{
string[] addressArray = tempAddress.Split(';');
string taddr1,taddr2;
taddr1=addressArray[0];
Console.Write(taddr1);
taddr2=addressArray[1];
Console.Write(taddr2);
}
Console.Write(GetProperty(sResultSet,"title"));
Console.Write(GetProperty(sResultSet,"company"));
Console.Write(GetProperty(sResultSet,"st"));
Console.Write(GetProperty(sResultSet,"l"));
Console.Write(GetProperty(sResultSet,"co"));
Console.Write(GetProperty(sResultSet,"postalCode"));
Console.Write(GetProperty(sResultSet,"telephoneNumber"));
Console.Write(GetProperty(sResultSet,"otherTelephone"));
Console.Write(GetProperty(sResultSet,"facsimileTelephoneNumber"));
Console.Write(GetProperty(sResultSet,"mail"));
Console.Write(GetProperty(sResultSet,"extensionAttribute1"));
Console.Write(GetProperty(sResultSet,"extensionAttribute2"));
Console.Write(GetProperty(sResultSet,"extensionAttribute3"));
Console.Write(GetProperty(sResultSet,"extensionAttribute4"));
Console.Write(GetProperty(sResultSet,"extensionAttribute5"));
Console.Write(GetProperty(sResultSet,"extensionAttribute6"));
Console.Write(GetProperty(sResultSet,"extensionAttribute7"));
Console.Write(GetProperty(sResultSet,"extensionAttribute8"));
String tEamil = GetProperty(sResultSet,"extensionAttribute9");
if(tEamil!=string.Empty)
{
string em1,em2,em3;
string[] emailArray = tEmail.Split(';');
em1=emailArray[0];
em2=emailArray[1];
em3=emailArray[2];
Console.Write(em1+em2+em3);
}
Console.Write(GetProperty(sResultSet,"extensionAttribute10"));
Console.Write(GetProperty(sResultSet,"extensionAttribute11"));
Console.Write(GetProperty(sResultSet,"extensionAttribute12"));
Console.Write(GetProperty(sResultSet,"whenCreated"));
Console.Write(GetProperty(sResultSet,"whenChanged"));
}
Enjoy.