Return to Snippet

Revision: 21688
at December 18, 2009 11:03 by blackf0rk


Updated Code
string sAMAccountName = "<login name>";
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", sAMAccountName);
string[] properties = new string[] { "fullname" };
//you must provide a username and password to authenticate (this is not the username and password of the person you're looking for):
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + "<domain>", "<username>", "<password>");
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry de = result.GetDirectoryEntry();

//you can now get the properties of the directory entry this way:
string property = de.Properties["<property name>"].Value.ToString();

/*some examples of property names:
co (country)
c (country abbreviation)
countryCode
mail
givenName (first name)
title
mobile
sAMAccountName
sn (last name)
st (state)
street
streetAddress
telephoneNumber
postalCode
*/

Revision: 21687
at December 18, 2009 11:00 by blackf0rk


Initial Code
string sAMAccountName = "<login name>";
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", sAMAccountName);
string[] properties = new string[] { "fullname" };
//you must provide a username and password to authenticate (this is not the username and password of the person you're looking for):
DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + "<domain>", "<username>", "<password>");
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry de = result.GetDirectoryEntry();

//you can now get the properties of the directory entry this way:
string property = de.Properties[property].Value.ToString();

Initial URL

                                

Initial Description

                                

Initial Title
Get a user from Active Directory

Initial Tags
directory

Initial Language
C#