Posted By

blackf0rk on 12/18/09


Tagged

directory active users ActiveDirectory person ad


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

emregulcan


Get a user from Active Directory


 / Published in: C#
 

  1. string sAMAccountName = "<login name>";
  2. string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", sAMAccountName);
  3. string[] properties = new string[] { "fullname" };
  4. //you must provide a username and password to authenticate (this is not the username and password of the person you're looking for):
  5. DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + "<domain>", "<username>", "<password>");
  6. DirectorySearcher searcher = new DirectorySearcher(adRoot);
  7. searcher.SearchScope = SearchScope.Subtree;
  8. searcher.ReferralChasing = ReferralChasingOption.All;
  9. searcher.PropertiesToLoad.AddRange(properties);
  10. searcher.Filter = filter;
  11. SearchResult result = searcher.FindOne();
  12. DirectoryEntry de = result.GetDirectoryEntry();
  13.  
  14. //you can now get the properties of the directory entry this way:
  15. string property = de.Properties["<property name>"].Value.ToString();
  16.  
  17. /*some examples of property names:
  18. co (country)
  19. c (country abbreviation)
  20. countryCode
  21. mail
  22. givenName (first name)
  23. title
  24. mobile
  25. sAMAccountName
  26. sn (last name)
  27. st (state)
  28. street
  29. streetAddress
  30. telephoneNumber
  31. postalCode
  32. */

Report this snippet  

You need to login to post a comment.