Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. using System;
  2. using System.DirectoryServices;
  3. using System.DirectoryServices.AccountManagement;
  4. using System.Linq;
  5.  
  6. namespace ADSamples
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. GetGroups(x => { Console.WriteLine(x.DistinguishedName); return true; });
  13. #if DEBUG
  14. Console.Write("Press any key... ");
  15. Console.ReadKey();
  16. #endif
  17. }
  18. public static void GetGroups(Func<GroupPrincipal, bool> func, ContextType contextType = ContextType.Domain, string name = null, string container = null, ContextOptions options = ContextOptions.Negotiate | ContextOptions.Signing | ContextOptions.Sealing, string userName = null, string password = null, int pageSize = 1000, SearchScope searchScope = SearchScope.Subtree)
  19. {
  20. using (PrincipalContext context = new PrincipalContext(contextType, name, container, options, userName, password))
  21. {
  22. using (GroupPrincipal principal = new GroupPrincipal(context))
  23. {
  24. principal.GroupScope = GroupScope.Global;
  25. using (PrincipalSearcher principalSearcher = new PrincipalSearcher(principal))
  26. {
  27. DirectorySearcher directorySearcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
  28. directorySearcher.PageSize = pageSize;
  29. directorySearcher.SearchScope = searchScope;
  30. foreach (Principal result in principalSearcher.FindAll())
  31. {
  32. if (func != null && !func(result as GroupPrincipal))
  33. break;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement