public string[] getUserDetails(string Username, string strPwd, string strAccountName)
{
string[] resultSet = new string[6];
string strUsername = string.Empty;
//string domainAndUsername = strDomain + @"\" + strUsername;
DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, strPwd);
try
{
DirectorySearcher search = new DirectorySearcher(entry);
if (strAccountName.Contains(@"\"))
{
strUsername = strAccountName.Substring(strAccountName.IndexOf(@"\") + 1);
}
else if (strAccountName.Contains("@"))
{
strUsername = strAccountName.Substring(0, strAccountName.IndexOf("@"));
}
else
{
strUsername = strAccountName;
}
search.Filter = "(SAMAccountName=" + strUsername + ")";
search.PropertiesToLoad.Add("Email");
search.PropertiesToLoad.Add("Department");
search.PropertiesToLoad.Add("Manager");
search.PropertiesToLoad.Add("Location");
search.PropertiesToLoad.Add("Telephone");
SearchResult result = search.FindOne();
resultSet[0] = (result.Properties["Email"]).Count == 0 ? string.Empty : (string)result.Properties["Email"][0]; ;
resultSet[1] = (result.Properties["Department"]).Count == 0 ? string.Empty : (string)result.Properties["Department"][0];
resultSet[2] = (result.Properties["Manager"]).Count == 0 ? string.Empty : (string)result.Properties["Manager"][0];
resultSet[3] = (result.Properties["Location"]).Count == 0 ? string.Empty : (string)result.Properties["Location"][0];
resultSet[4] = (result.Properties["Telephone"]).Count == 0 ? string.Empty : (string)result.Properties["Telephone"][0];
if (null == result)
{
return resultSet;
}
//Update the new path to the user in the directory.
_path = result.Path;
_filterAttribute = (string)result.Properties["cn"][0];
}
catch (Exception ex)
{
throw ex;
}
return resultSet;
}This code describe how to get userdetails from active directory.
