/// <summary> /// Search in AD for non-eRSA accounts created after a given date /// Only display selected attributes defined in _essentialProperties /// </summary> /// <param name="earliest"></param> public List <Dictionary <string, string> > Search(DateTime earliest) { string whenCreated = earliest.ToUniversalTime().ToString("yyyyMMddHHmmss.0Z"); Console.WriteLine("Local {0} to UTC {1}", earliest, whenCreated); string userFilter = HELPER.CreateFilter(whenCreated); List <Dictionary <string, string> > results = new List <Dictionary <string, string> >(); LdapSearchResults lsc = conn.Search(HELPER.SEARCH_BASE, LdapConnection.SCOPE_SUB, userFilter, HELPER.CREATION_PROPERTIES, false); int count = 0; while (lsc.hasMore()) { LdapEntry nextEntry = null; try { nextEntry = lsc.next(); count++; } catch (LdapReferralException) { // Nothing really serious: constraints.ReferralFollowing = true this may not be needed // https://www.novell.com/documentation/developer/ldapcsharp/?page=/documentation/developer/ldapcsharp/cnet/data/b3u4u0n.html // https://technet.microsoft.com/en-us/library/cc978014.aspx continue; } catch (LdapException e) { Console.WriteLine("Move next error: {0}", e.ToString()); Console.WriteLine("Error message: " + e.Message); continue; } Console.WriteLine("\n" + nextEntry.DN); try { results.Add(GetProperties(nextEntry.getAttributeSet())); } catch (NullReferenceException ex) { Console.WriteLine("Not a qualified person account"); Console.WriteLine(ex.Message); } } return(results); }
// Install-Package System.DirectoryServices -Source https://dotnet.myget.org/F/dotnet-core/api/v3/index.json /// <summary> /// Search non-eRSA user created after given earliest /// Only display selected attributes defined in HELPER.CREATION_PROPERTIES /// </summary> /// <param name="earliest"></param> public List <Dictionary <string, string> > Search(DateTime earliest) { Console.WriteLine($"Start to search for accounts created after {earliest}"); Console.WriteLine(_domain.Name); List <Dictionary <string, string> > results = new List <Dictionary <string, string> >(); try { // Format DateTime object to a datetime string in AD-LDAP format // https://social.technet.microsoft.com/wiki/contents/articles/28222.active-directory-generalized-time-attributes.aspx // https://stackoverflow.com/questions/10391174/query-ldap-for-all-computer-objects-created-in-the-last-24-hours string whenCreated = earliest.ToUniversalTime().ToString("yyyyMMddHHmmss.0Z"); Console.WriteLine("Local {0} to UTC {1}", earliest, whenCreated); int count = 0; // https://technet.microsoft.com/en-us/library/aa996205(v=exchg.65).aspx#DoingASearchUsingADUC string userFilter = HELPER.CreateFilter(whenCreated); using (DirectorySearcher newAccounts = new DirectorySearcher(_domain, userFilter, HELPER.CREATION_PROPERTIES)) { foreach (SearchResult res in newAccounts.FindAll()) { try { results.Add(GetProperties(res)); count++; } catch (NullReferenceException ex) { Console.WriteLine(ex.Message); } } Console.WriteLine($"Total user found: {count}"); } } catch (System.Exception ex) { Console.WriteLine("Search failed, see exception below:"); Console.WriteLine(ex.ToString()); } return(results); }