Tuesday, August 28, 2007

Connecting to Active Directory - AD - using c#

Hi all, This is one of the most important and basic things a developer has to do in his life. Here I will show how to connect to the AD and get some basic information required.

I also have covered fetching manager's information in this topic, I faced problem while working with this. When you search AD with UserID, it returns a path to the manager's information. We have to use the path and search the AD again to get manager's mailid and other information...


public string GetManagerMailID(string UserName)
{
try
{
Hashtable hsEmpData;
string sManagersMailID = "";

hsEmpData = SearchLDAP(UserName);
foreach (DictionaryEntry deSearchDataKey in hsEmpData)
{
if (deSearchDataKey.Key.ToString() == "manager")
{
sManagersMailID = getManagersID(deSearchDataKey.Value.ToString());
break;
}
}
return sManagersMailID;
}
catch (Exception ex)
{
throw ex;
}
}




private static Hashtable SearchLDAP(string userID)
{
//Pass a connecetion string of sorts to the DirectoryEntry,
//Here replace ABC with domain name...
DirectoryEntry entry = new DirectoryEntry("LDAP://DC=ABC,DC=com");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = "(&(objectClass=user)(anr="+ userID +"))";

mySearcher.PropertiesToLoad.Add("givenname"); // will give you first name
mySearcher.PropertiesToLoad.Add("sn"); // will give you last name
mySearcher.PropertiesToLoad.Add("mail"); // for mail id
mySearcher.PropertiesToLoad.Add("manager"); // for manager's information

Hashtable associateDetailsTable = new Hashtable();
ResultPropertyValueCollection resultCollection;

//search the AD depending on userID
SearchResult searchResult = mySearcher.FindOne();

//Add the details to the hashtable...
associateDetailsTable.Add("AssociateID", userID);
if(searchResult != null)
{
resultCollection = searchResult.Properties["givenname"];
foreach(object result in resultCollection)
{
associateDetailsTable.Add("FirstName", result.ToString());
break;
}
resultCollection = searchResult.Properties["sn"];
foreach(object result in resultCollection)
{
associateDetailsTable.Add("LastName", result.ToString());
break;
}
resultCollection = searchResult.Properties["mail"];
foreach(object result in resultCollection)
{
associateDetailsTable.Add("Mail", result.ToString());
break;
}
resultCollection = searchResult.Properties["manager"];
foreach (object result in resultCollection)
{
associateDetailsTable.Add("manager", result.ToString());
break;
}
}
return associateDetailsTable;
}




private string getManagersID(string managersString)
{
DirectoryEntry entry = new DirectoryEntry("LDAP://" + managersString);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
//mySearcher.Filter = "(&(objectClass=user)(anr=" + userID + "))";

string sMailID="";

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

ResultPropertyValueCollection resultCollection;
SearchResult searchResult = mySearcher.FindOne();

if (searchResult != null)
{
resultCollection = searchResult.Properties["mail"];
foreach (object result in resultCollection)
{
sMailID = result.ToString();
break;
}
}
return sMailID;

}


- Vighnesh Bendre

Hope this post helps atleast one person.
For any doubts contact me at vikram.bendre@gmail.com