code_junkie

Quick way to retrieve user information Active Directory

Nov 13th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3. SearchResultCollection sResults = null;
  4.  
  5. try
  6. {
  7. //modify this line to include your domain name
  8. string path = "LDAP://EXTECH";
  9. //init a directory entry
  10. DirectoryEntry dEntry = new DirectoryEntry(path);
  11.  
  12. //init a driectory searcher
  13. DirectorySearcher dSearcher = new DirectorySearcher(dEntry);
  14.  
  15. //This line applies a filter to the search specifying a username to search for
  16. //modify this line to specify a user name. if you want to search for all
  17. //users who start with k - set SearchString to "k"
  18. dSearcher.Filter = "(&(objectClass=user))";
  19.  
  20. //perform search on active directory
  21. sResults = dSearcher.FindAll();
  22.  
  23. //loop through results of search
  24. foreach (SearchResult searchResult in sResults)
  25. {
  26. if (searchResult.Properties["CN"][0].ToString() == "Adit")
  27. {
  28. ////loop through the ad properties
  29. //foreach (string propertyKey in
  30. //searchResult.Properties["st"])
  31. //{
  32.  
  33. //pull the collection of objects with this key name
  34. ResultPropertyValueCollection valueCollection =
  35. searchResult.Properties["manager"];
  36.  
  37. foreach (Object propertyValue in valueCollection)
  38. {
  39.  
  40. //loop through the values that have a specific name
  41. //an example of a property that would have multiple
  42. //collections for the same name would be memberof
  43. //Console.WriteLine("Property Name: " + valueCollection..ToString());
  44. Console.WriteLine("Property Value: " + (string)propertyValue.ToString());
  45.  
  46. //["sAMAccountName"][0].ToString();
  47. }
  48. //}
  49. Console.WriteLine(" ");
  50. }
  51. }
  52. }
  53. catch (InvalidOperationException iOe)
  54. {
  55. //
  56. }
  57. catch (NotSupportedException nSe)
  58. {
  59. //
  60. }
  61. finally
  62. {
  63.  
  64. // dispose of objects used
  65. if (sResults != null)
  66. sResults.Dispose();
  67.  
  68. }
  69. Console.ReadLine();
  70. }
  71.  
  72. dSearcher.Filter = "(&(objectClass=user))";
  73.  
  74. dSearcher.Filter = "(&(objectClass=user)(cn=Adit))";
  75.  
  76. DirectorySearcher searcher = new DirectorySearcher();
  77. searcher.Filter = "(&(objectCategory=user)(cn=steve.evans))";
  78.  
  79. SearchResultCollection results = searcher.FindAll();
  80.  
  81. if (results.Count == 1)
  82. {
  83. //do what you want to do
  84. }
  85. else if (results.Count == 0)
  86. {
  87. //user does not exist
  88. }
  89. else
  90. {
  91. //found more than one user
  92. //something is wrong
  93. }
  94.  
  95. DirectoryEntry deUser = new DirectoryEntry("LDAP://cn=John Doe,cn=Users,dc=yourdomain,dc=com");
  96.  
  97. if (deUser != null)
  98. {
  99. ... do something with your user
  100. }
  101.  
  102. // bind to your domain
  103. PrincipalContext pc = new PrincipalContext(ContextType.Domain, "LDAP://dc=yourdomain,dc=com");
  104.  
  105. // find the user by identity (or many other ways)
  106. UserPrincipal user = UserPrincipal.FindByIdentity(pc, "cn=John Doe");
Add Comment
Please, Sign In to add comment