SHARE
TWEET

Untitled

a guest Apr 21st, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices;
  4. using System.DirectoryServices.AccountManagement;
  5. using System.Linq;
  6.  
  7. public static class DirectoryServiceUtils
  8. {
  9.     private static IList<string> _DomainGroupList = null;
  10.     public static IList<string> DomainGroupList
  11.     {
  12.         get
  13.         {
  14.             if (_DomainGroupList == null)
  15.             {
  16.                 _DomainGroupList = new List<string>();
  17.                 GetGroups(x => { _DomainGroupList.Add(x.SamAccountName); return true; });
  18.             }
  19.             return _DomainGroupList;
  20.         }
  21.     }
  22.     public static IList<Principal> GetGroups(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)
  23.     {
  24.         using (PrincipalContext context = new PrincipalContext(contextType, name, container, options, userName, password))
  25.         {
  26.             using (GroupPrincipal principal = new GroupPrincipal(context))
  27.             {
  28.                 principal.GroupScope = GroupScope.Global;
  29.                 using (PrincipalSearcher principalSearcher = new PrincipalSearcher(principal))
  30.                 {
  31.                     DirectorySearcher directorySearcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
  32.                     directorySearcher.PageSize = pageSize;
  33.                     directorySearcher.SearchScope = searchScope;
  34.                     return principalSearcher.FindAll().ToList();
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     public static string GetGroups(string separator = ",", 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)
  40.     {
  41.         return String.Join(separator, GetGroups(contextType, name, container, options, userName, password, pageSize, searchScope).Select(x => x.SamAccountName).ToArray());
  42.     }
  43.     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)
  44.     {
  45.         using (PrincipalContext context = new PrincipalContext(contextType, name, container, options, userName, password))
  46.         {
  47.             using (GroupPrincipal principal = new GroupPrincipal(context))
  48.             {
  49.                 principal.GroupScope = GroupScope.Global;
  50.                 using (PrincipalSearcher principalSearcher = new PrincipalSearcher(principal))
  51.                 {
  52.                     DirectorySearcher directorySearcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
  53.                     directorySearcher.PageSize = pageSize;
  54.                     directorySearcher.SearchScope = searchScope;
  55.                     foreach (Principal result in principalSearcher.FindAll())
  56.                     {
  57.                         if (func != null && !func(result as GroupPrincipal))
  58.                             break;
  59.                     }
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top