Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
public static DirectoryEntry GetDirectoryEntry()
{
DirectoryEntry de = new DirectoryEntry("LDAP://19dc01", "USERNAME", "PASSWORD");
return de;
}
public bool UserExists(string username)
{
DirectoryEntry de = GetDirectoryEntry();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user) (cn=" + username + "))";
SearchResultCollection results = deSearch.FindAll();
return results.Count > 0;
}
public String GetProperty(String userAccount, String propertyName)
{
DirectoryEntry entry = GetDirectoryEntry();
String account = userAccount.Replace(@"DOMAIN\", "");
try
{
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + account + ")";
search.PropertiesToLoad.Add(propertyName);
SearchResult result = search.FindOne();
if (result != null)
{
return result.Properties[propertyName][0].ToString();
}
else
{
return "Unknown User";
}
}
catch (Exception ex)
{
string debug = ex.Message;
return "";
}
}
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
Join the community of 500,000 technology professionals and ask your questions.