Esempio n. 1
0
        /// <summary>
        /// Gets
        /// </summary>
        /// <param name="samAccountName"></param>
        /// <param name="displayName"></param>
        /// <returns></returns>
        internal static IList <UserPrincipal> Get(string samAccountName, string displayName)
        {
            if (samAccountName == null && displayName == null)
            {
                throw new ArgumentException("At least one parameter is required.");
            }
            List <UserPrincipal> userPrincipalList = new List <UserPrincipal>();
            UserPrincipal        userPrincipal1    = new UserPrincipal(new PrincipalContext(ContextType.Domain, DomainsManager.GetContextDomain()));

            if (samAccountName != null)
            {
                Utils.validateLength(samAccountName);
                userPrincipal1.SamAccountName = samAccountName;
            }
            if (displayName != null)
            {
                Utils.validateLength(displayName);
                userPrincipal1.DisplayName = displayName;
            }
            using (PrincipalSearcher principalSearcher = new PrincipalSearcher((Principal)userPrincipal1))
            {
                foreach (UserPrincipal userPrincipal2 in principalSearcher.FindAll())
                {
                    userPrincipalList.Add(userPrincipal2);
                }
            }
            return((IList <UserPrincipal>)userPrincipalList);
        }
        /// <summary>
        /// Gets all the computers from Active Directory
        /// </summary>
        /// <returns></returns>
        internal static List <ComputerPrincipal> GetAll()
        {
            List <ComputerPrincipal> computerPrincipalList = new List <ComputerPrincipal>();

            using (PrincipalSearcher principalSearcher = new PrincipalSearcher((Principal) new ComputerPrincipal(new PrincipalContext(ContextType.Domain, DomainsManager.GetContextDomain()))))
            {
                foreach (ComputerPrincipal computerPrincipal in principalSearcher.FindAll())
                {
                    computerPrincipalList.Add(computerPrincipal);
                }
            }
            return(computerPrincipalList);
        }