0
Reply

UserPrincipal.FindByPasswordSetTime Method

Ask a question
Darren Young

Darren Young

14y
2.7k
1
I have the following method that works but always returns excessively large result sets. We have around 38,000 user objects in AD and based on queries of PwdLastSet many of the entries returned by this method clearly don't expire. Thoughts?

        public static List<string> getExpiringPasswordEntries(int days)
        {
            MethodBase mbo = System.Reflection.MethodBase.GetCurrentMethod();
            Debug.WriteLine(mbo.Name + ": Entering");

            List<string> returnList = new List<string>();
            DateTime dt = DateTime.Today.AddDays(days);

            Debug.Print(mbo.Name + ": dt val " + dt.ToString());
            Debug.Print(mbo.Name + ": getting list of passwords that expire in " + days + " days");

            try
            {
                PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, "gsb.uchicago.edu");
                PrincipalSearchResult<UserPrincipal> results =
                    UserPrincipal.FindByPasswordSetTime(
                    adPrincipalContext,
                    dt,
                    MatchType.LessThanOrEquals);

                foreach (UserPrincipal result in results)
                {
                    returnList.Add(result.SamAccountName);
                    //Debug.WriteLine(mbo.Name + ": " + result.SamAccountName);
                }

                Debug.WriteLine(mbo.Name + ": Returning");
                return returnList;
            }
            catch
            {
                throw;
            }

        }