public static bool ActiveDirectoryValidation(string userName, DirectoryEntry entry,out string errorMessage) { errorMessage = ""; if (String.IsNullOrEmpty(userName)) return false; var result = false; var account = new AccountInfo(); var search = new DirectorySearcher(entry); var extensionAttributesName = ApplicationConfiguration.ActiveDirectoryAttributeName.Split('|'); var extensionAttributesValue = ApplicationConfiguration.ActiveDirectoryAttributeValue.Split('|'); search.Filter = "(SAMAccountName=" + userName + ")"; search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("mail"); search.PropertiesToLoad.Add("department"); search.PropertiesToLoad.Add("physicalDeliveryOfficeName"); foreach (string extension in extensionAttributesName) search.PropertiesToLoad.Add(extension); var searchResult = search.FindOne(); for (int i = 0; i < extensionAttributesName.Length; i++) { if (searchResult.Properties[extensionAttributesName[i]].Count == 0) { errorMessage = "LDAPError"; return false; } if (searchResult.Properties[extensionAttributesName[i]][0].ToString().ToUpper() == extensionAttributesValue[i].ToString().ToUpper()) { if (searchResult.Properties["cn"].Count > 0) { account.SetFullName((String)searchResult.Properties["cn"][0]); account.SetEmailAddress((String)searchResult.Properties["mail"][0]); account.SetDepartment((string)searchResult.Properties["department"][0]); account.SetLocation((string)searchResult.Properties["physicalDeliveryOfficeName"][0]); result = true; } } } return result; }