Inheritance: System.DirectoryServices.DirectorySearcher
コード例 #1
0
        public virtual IReadOnlyCollection <IPrincipal> GetGroupMembers(string groupName, bool recursive, ICollection <string> additionalPropertyNames = null)
        {
            List <IPrincipal> result = new List <IPrincipal>();

            foreach (DomainPath domainPath in domainPaths)
            {
                using (DirectoryEntry domainEntry = new DirectoryEntry(domainPath.GetPathWithProtocol()))
                    using (PrincipalSearcher principalSearcher = new PrincipalSearcher(domainPath, domainEntry))
                    {
                        GroupPrincipal groupPrincipal = principalSearcher.FindPrincipal <GroupPrincipal>(groupName);
                        result.AddRange(ResolveMembers(groupPrincipal, recursive, additionalPropertyNames));
                    }
            }

            return(result);
        }
コード例 #2
0
        public virtual IReadOnlyCollection <IPrincipal> SearchPrincipals(string ldapFilter, ICollection <string> additionalPropertyNames = null)
        {
            foreach (DomainPath domainPath in domainPaths)
            {
                using (DirectoryEntry domainEntry = new DirectoryEntry(domainPath.GetPathWithProtocol()))
                    using (PrincipalSearcher searcher = new PrincipalSearcher(domainPath, domainEntry, additionalPropertyNames))
                    {
                        IReadOnlyCollection <Principal> principals = searcher.FindPrincipals <Principal>(ldapFilter);
                        if (principals.Any())
                        {
                            return(principals);
                        }
                    }
            }

            return(new List <IPrincipal>(0));
        }
コード例 #3
0
        protected virtual T GetPrincipalInternal <T>(string accountName, ICollection <string> additionalPropertyNames = null) where T : Principal
        {
            string domain = GetDomain(accountName);

            foreach (DomainPath domainPath in domains.Where(o => domain == null || o.Value.Equals(domain, StringComparison.OrdinalIgnoreCase)).Select(o => o.Key))
            {
                using (DirectoryEntry domainEntry = new DirectoryEntry(domainPath.GetPathWithProtocol()))
                    using (PrincipalSearcher searcher = new PrincipalSearcher(domainPath, domainEntry, additionalPropertyNames))
                    {
                        try
                        {
                            return(searcher.FindPrincipal <T>(accountName));
                        }
                        catch (ActiveDirectoryException)
                        {
                        }
                    }
            }

            throw new ActiveDirectoryException(string.Format("Principal '{0}' not found in active directory.", accountName));
        }