Get Userdetails from Active Directory

  1. public string[] getUserDetails(string Username, string strPwd, string strAccountName)  
  2.        {  
  3.            string[] resultSet = new string[6];  
  4.            string strUsername = string.Empty;  
  5.            //string domainAndUsername = strDomain + @"\" + strUsername;  
  6.            DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, strPwd);  
  7.   
  8.            try  
  9.            {  
  10.                  
  11.   
  12.                DirectorySearcher search = new DirectorySearcher(entry);  
  13.                if (strAccountName.Contains(@"\"))  
  14.                {  
  15.                    strUsername = strAccountName.Substring(strAccountName.IndexOf(@"\") + 1);  
  16.                }  
  17.                else if (strAccountName.Contains("@"))  
  18.                {  
  19.                    strUsername = strAccountName.Substring(0, strAccountName.IndexOf("@"));  
  20.                }  
  21.                else   
  22.                {  
  23.                    strUsername = strAccountName;  
  24.                }  
  25.                search.Filter = "(SAMAccountName=" + strUsername + ")";  
  26.   
  27.                search.PropertiesToLoad.Add("Email");  
  28.                
  29.                search.PropertiesToLoad.Add("Department");  
  30.                search.PropertiesToLoad.Add("Manager");  
  31.                search.PropertiesToLoad.Add("Location");  
  32.                search.PropertiesToLoad.Add("Telephone");  
  33.   
  34.                SearchResult result = search.FindOne();  
  35.   
  36.                resultSet[0] = (result.Properties["Email"]).Count == 0 ? string.Empty : (string)result.Properties["Email"][0]; ;  
  37.                 
  38.                resultSet[1] = (result.Properties["Department"]).Count == 0 ? string.Empty : (string)result.Properties["Department"][0];  
  39.                resultSet[2] = (result.Properties["Manager"]).Count == 0 ? string.Empty : (string)result.Properties["Manager"][0];  
  40.                
  41.                resultSet[3] = (result.Properties["Location"]).Count == 0 ? string.Empty : (string)result.Properties["Location"][0];  
  42.                resultSet[4] = (result.Properties["Telephone"]).Count == 0 ? string.Empty : (string)result.Properties["Telephone"][0];  
  43.                if (null == result)  
  44.                {  
  45.                    return resultSet;  
  46.                }  
  47.   
  48.                //Update the new path to the user in the directory.  
  49.                _path = result.Path;  
  50.                _filterAttribute = (string)result.Properties["cn"][0];  
  51.            }  
  52.            catch (Exception ex)  
  53.            {  
  54.                throw ex;  
  55.            }  
  56.   
  57.            return resultSet;  
  58.        }