// Connect to LDAP
AuthenticationTypes authTypes; // Authentication flags.
authTypes = AuthenticationTypes.Signing | AuthenticationTypes.Sealing | AuthenticationTypes.Secure;
string domainAndUsername = domain + @"\" + user;
DirectoryEntry oAuthedEntry = new DirectoryEntry(ldapConnectionString, domainAndUsername, pwd, authTypes);
// Create new DirectorySearcher instance
DirectorySearcher search = new DirectorySearcher(oAuthedEntry);
// Set search criteria
search.Filter = "(objectCategory=user)";
search.PropertiesToLoad.Add("displayName"); // name
search.PropertiesToLoad.Add("physicalDeliveryOfficeName"); // location
search.PropertiesToLoad.Add("telephoneNumber"); // phone number
search.PropertiesToLoad.Add("mobile"); // mobile
search.PropertiesToLoad.Add("mail"); // email address
search.PropertiesToLoad.Add("msDS-UserAccountDisabled"); // account status
search.PropertiesToLoad.Add("title"); // title
search.PropertiesToLoad.Add("ms-DS-UserAccountAutoLocked"); // locked
// Perform the search
SearchResultCollection results = null;
try
{
results = search.FindAll();
}
catch (Exception searchEx)
{
throw new Exception("Error obtaining users results. ", searchEx);
}