Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.DirectoryServices;
- using System.DirectoryServices.AccountManagement;
- using System.Linq;
- public static class DirectoryServiceUtils
- {
- private static IList<string> _DomainGroupList = null;
- public static IList<string> DomainGroupList
- {
- get
- {
- if (_DomainGroupList == null)
- {
- _DomainGroupList = new List<string>();
- GetGroups(x => { _DomainGroupList.Add(x.SamAccountName); return true; });
- }
- return _DomainGroupList;
- }
- }
- 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)
- {
- using (PrincipalContext context = new PrincipalContext(contextType, name, container, options, userName, password))
- {
- using (GroupPrincipal principal = new GroupPrincipal(context))
- {
- principal.GroupScope = GroupScope.Global;
- using (PrincipalSearcher principalSearcher = new PrincipalSearcher(principal))
- {
- DirectorySearcher directorySearcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
- directorySearcher.PageSize = pageSize;
- directorySearcher.SearchScope = searchScope;
- return principalSearcher.FindAll().ToList();
- }
- }
- }
- }
- 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)
- {
- return String.Join(separator, GetGroups(contextType, name, container, options, userName, password, pageSize, searchScope).Select(x => x.SamAccountName).ToArray());
- }
- 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)
- {
- using (PrincipalContext context = new PrincipalContext(contextType, name, container, options, userName, password))
- {
- using (GroupPrincipal principal = new GroupPrincipal(context))
- {
- principal.GroupScope = GroupScope.Global;
- using (PrincipalSearcher principalSearcher = new PrincipalSearcher(principal))
- {
- DirectorySearcher directorySearcher = principalSearcher.GetUnderlyingSearcher() as DirectorySearcher;
- directorySearcher.PageSize = pageSize;
- directorySearcher.SearchScope = searchScope;
- foreach (Principal result in principalSearcher.FindAll())
- {
- if (func != null && !func(result as GroupPrincipal))
- break;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement