Exemplo n.º 1
0
        public List <GroupPrincipalFull> GetChildGroupObjects(int maxRecords)
        {
            var directoryDe = new DirectoryEntry();

            if (ContextType == ContextType.ApplicationDirectory)
            {
                if (!string.IsNullOrEmpty(Container) &&
                    !string.IsNullOrEmpty(Name))
                {
                    directoryDe = new DirectoryEntry(string.Format("LDAP://{0}/{1}", Name, Container));
                }
                else
                {
                    directoryDe = new DirectoryEntry(string.Format("LDAP://{0}", Name));
                }
            }
            if (ContextType == ContextType.Domain)
            {
                directoryDe = new DirectoryEntry(string.Format("LDAP://{0}", ConnectedServer));
            }
            if (ContextType == ContextType.Machine)
            {
                throw new NotSupportedException(
                          "This functionality is not available for Machine Context Type PrincipalContext objects.");
            }
            var search = new DirectorySearcher(directoryDe)
            {
                Tombstone    = false,
                Asynchronous = true,
                PageSize     = 100,
                Filter       = "(objectClass=group)"
            };
            var results  = search.FindAll();
            var i        = 0;
            var children = new List <GroupPrincipalFull>();

            foreach (SearchResult result in results)
            {
                i++;
                var delims = new[] { '/' };
                var pieces = result.Path.Split(delims);
                var dn     = pieces[pieces.Count() - 1];

                if (maxRecords > 0 && i > maxRecords)
                {
                    break;
                }
                try
                {
                    children.Add(GroupPrincipalFull.FindByIdentity(this, IdentityType.DistinguishedName, dn));
                }
                catch
                {
                }
            }
            return(children);
        }