示例#1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if ( !IsPostBack )
        {
            fname.Attributes.Add( "readonly", "readonly" );
            lname.Attributes.Add( "readonly", "readonly" );

            email.Focus();
            populate();

            foreach ( ListItem room in epaRoomNums )
            {
                epaRooms.Items.Add( room );
            }
            foreach ( ListItem room in kRoomNums )
            {
                kRooms.Items.Add( room );
            }
            foreach ( ListItem room in epaTrainNums )
            {
                epaTraining.Items.Add( room );
            }

            using ( PrincipalContext pc = new PrincipalContext( System.DirectoryServices.AccountManagement.ContextType.Domain, "ITSERVICES" ) )
            {
                using ( UserPrincipal user = new UserPrincipal( pc ) )
                {
                    user.EmailAddress = "*";
                    using ( PrincipalSearcher ps = new PrincipalSearcher() )
                    {
                        ps.QueryFilter = user;
                        ((DirectorySearcher)ps.GetUnderlyingSearcher()).PageSize = 500;
                        PrincipalSearchResult<Principal> psr = ps.FindAll();
                        AD_Users = new string[psr.Count()];

                        int i = 0;
                        foreach ( UserPrincipal u in psr )
                        {
                            AD_Users[i++] = u.EmailAddress.Split('@')[0];
                        }
                    }
                }
            }

           //Debug_FillForm();
        }
    }
示例#2
0
 protected void Page_Load(object sender, EventArgs e)
 {
     UserPanel.Visible = false;
     ComputerPanel.Visible = false;
     PrincipalContext pc = new PrincipalContext( ContextType.Domain, "ITSERVICES" );
     ComputerPrincipal computer = new ComputerPrincipal( pc );
     computer.Name = "*"; //reg expression
     PrincipalSearcher ps = new PrincipalSearcher();
     ps.QueryFilter = computer;
     ((System.DirectoryServices.DirectorySearcher)ps.GetUnderlyingSearcher()).PageSize = 500;
     PrincipalSearchResult<Principal> psr = ps.FindAll();
     AD_Computers = new string[psr.Count()];
     int i = 0;
     foreach ( ComputerPrincipal cp in psr )
     {
         AD_Computers[i++] = cp.Name;
     }
 }
示例#3
0
        public Dictionary<string, string> FindActiveDirectoryMatches(string term)
        {
            Principals = new List<UserPrincipal>();
            Wigglers = new List<Wiggler>(6000);

            var ctx = new PrincipalContext(ContextType.Domain, domainName);
            var user = new UserPrincipal(ctx) { DisplayName = term + "*", Enabled = true };
            var ps = new PrincipalSearcher { QueryFilter = user };
            ((DirectorySearcher)ps.GetUnderlyingSearcher()).PageSize = 3000;

            var result = ps.FindAll();
            Console.WriteLine(result.Count());
            foreach (UserPrincipal p in result)
            {
                if (string.IsNullOrEmpty(p.EmailAddress)) continue;
                Principals.Add(p);
                Wigglers.Add(new Wiggler { EmailAddress = p.EmailAddress, Name = p.Name });
            }

            return Wigglers.OrderBy(w => w.Name).Take(100).ToDictionary(wiggler => wiggler.EmailAddress, wiggler => wiggler.Name);
        }
示例#4
0
        public int GetActiveUserCount()
        {
            int result = 0;

            // Setting a filter so that the search we are about to do
            // only searches for users that are enabled in active directory
            UserPrincipalEx userPrincipal = new UserPrincipalEx(Context)
            {
                Enabled = true
            };

            PrincipalSearcher searcher = new PrincipalSearcher();
            searcher.QueryFilter = userPrincipal;
            ((DirectorySearcher)searcher.GetUnderlyingSearcher()).PageSize = 500;

            foreach(UserPrincipalEx principalResult in searcher.FindAll())
            {
                result++;
            }

            return result;
        }
示例#5
0
        /// <summary>
        /// Gets a list of all accounts that are active in the specified OU.
        /// </summary>
        /// <returns></returns>
        public List<ADUser> GetAllActiveUsers()
        {
            List<ADUser> users = new List<ADUser>();

            UserPrincipalEx userPrincipal = new UserPrincipalEx(Context)
            {
                Enabled = true
            };

            PrincipalSearcher searcher = new PrincipalSearcher();
            searcher.QueryFilter = userPrincipal;
            ((DirectorySearcher)searcher.GetUnderlyingSearcher()).PageSize = 1000;
            var searchResults = searcher.FindAll().ToList();

            foreach(UserPrincipalEx user in searchResults)
            {
                users.Add(new ADUser()
                {
                    UserName = user.SamAccountName,
                    FirstName = user.GivenName,
                    LastName = user.Surname,
                    Title = user.Title,
                    Email = user.EmailAddress,
                    Department = user.Department.Trim(),
                    Company = user.Company.Trim(),

                    // The WhenCreated field is stored as a generalized date string
                    // as such, we have to convert it to Local time to see the date/time
                    // that the account was created
                    WhenCreated = user.WhenCreated.ToLocalTime()
                });
            }

            return users;
        }
示例#6
0
        public List<ADUser> SearchForUsers(string searchString, string searchField)
        {
            // the wildcard character is the asterisks, therefore I will add
            // one before and after the search string.
            searchString = "*" + searchString + "*";

            List<ADUser> users = new List<ADUser>();
            UserPrincipalEx userPrincipal = new UserPrincipalEx(Context);
            userPrincipal.Enabled = true;

            switch(searchField)
            {
                case "department":
                    userPrincipal.Department = searchString;
                    break;
                case "firstName":
                    userPrincipal.GivenName = searchString;
                    break;
                case "lastName":
                    userPrincipal.Surname = searchString;
                    break;
                case "title":
                    userPrincipal.Title = searchString;
                    break;
                default:
                    break;
            }

            PrincipalSearcher searcher = new PrincipalSearcher();
            searcher.QueryFilter = userPrincipal;
            ((DirectorySearcher)searcher.GetUnderlyingSearcher()).PageSize = 500;

            foreach(UserPrincipalEx user in searcher.FindAll())
            {
                users.Add(new ADUser()
                {
                    UserName = user.SamAccountName,
                    FirstName = user.GivenName,
                    LastName = user.Surname,
                    Department = user.Department,
                    Title = user.Title,
                });
            }

            return users;
        }
示例#7
0
        /// <summary>
        /// Gets all principal objects matching the search principal.
        /// </summary>
        /// <param name="searchPrincipal">Principal to use as basis of search.</param>
        /// <param name="pageIndex">Zero-based index of page to return, or null for all results.</param>
        /// <param name="pageSize">Number of items per page to return, or null for all results.</param>
        /// <param name="sortOrder">Sort order for results, or null to sort by configuration IdentityType.</param>
        /// <returns>Collection of all matching principals.</returns>
        private ICollection<Principal> GetAllPrincipals(Principal searchPrincipal, int? pageIndex = null, int? pageSize = null, Nullable<IdentityType> sortOrder = null)
        {
            // Since parents that call this function are wrapped in retry loops, this function should not be.

            // Get principalSearch for this element.
            var principalSearcher = new PrincipalSearcher(searchPrincipal);

            // Configure settings for underlying searcher.
            var underlyingSearcher = (DirectorySearcher)principalSearcher.GetUnderlyingSearcher();
            underlyingSearcher.PageSize = 0x200;
            underlyingSearcher.PropertiesToLoad.Add(this.Config.IdentityType.ToString().ToLower());

            if (sortOrder.HasValue)
            {
                underlyingSearcher.Sort = new SortOption(sortOrder.Value.ToString().ToLower(), SortDirection.Ascending);
                underlyingSearcher.PropertiesToLoad.Add(sortOrder.Value.ToString().ToLower());
            }
            else
            {
                underlyingSearcher.Sort = new SortOption(this.Config.IdentityType.ToString().ToLower(), SortDirection.Ascending);
            }

            // Get and process results.
            var principals = new List<Principal>();
            var principalResults = principalSearcher.FindAll();

            // Calculate begin and end points for results.
            int startIndex = 0, endIndex = int.MaxValue;
            if ((pageIndex != null) && (pageSize != null))
            {
                startIndex = pageIndex.Value * pageSize.Value;
                endIndex = (pageIndex.Value + 1) * pageSize.Value;
            }

            // Use enumerator to loop because of issues with errors on sometimes-returned invalid SIDs.
            // See: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/9dd81553-3539-4281-addd-3eb75e6e4d5d
            var principalEnum = principalResults.GetEnumerator();
            int currentIndex = 0;
            while (principalEnum.MoveNext())
            {
                if ((startIndex <= currentIndex) && (currentIndex < endIndex))
                {
                    Principal newPrincipal = null;
                    try
                    {
                        newPrincipal = principalEnum.Current;

                        if (newPrincipal != null)
                        {
                            // Add group object to results.
                            principals.Add(newPrincipal);
                        }
                    }
                    catch (PrincipalOperationException pe)
                    {
                        continue;
                    }
                }

                // Increment counter.
                currentIndex++;
            }

            return principals;
        }