Link to home
Start Free TrialLog in
Avatar of jasonbrandt3
jasonbrandt3

asked on

C# Filter AD search

I'm searching AD for user accounts that were created on or after a certain date, I'd like to just pull in the login id (samAccount).  Not sure how to modify the attached code to accomplish this.  Learning C# on the fly!  Thanks for any assistance.
public class MainClass {
    public static void Main() {
        using (DirectoryEntry de = new DirectoryEntry("LDAP://yourV/OU=yourV, DC=explorer, DC=local"))
        using (DirectorySearcher searcher = new DirectorySearcher()) {
            de.Username = @"explorer\yourName";
            de.Password = "password";
            searcher.SearchRoot = de;
            searcher.Filter = "(&(objectClass=user)(description=Auth*))";
            searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("name");
            searcher.PropertiesToLoad.Add("description");
            searcher.PropertiesToLoad.Add("givenName");
            searcher.PropertiesToLoad.Add("wWWHomePage");
            searcher.Sort = new SortOption("givenName", SortDirection.Ascending);
            SearchResultCollection results = searcher.FindAll();
            foreach (SearchResult result in results) {
                ResultPropertyCollection props = result.Properties;
                foreach (string propName in props.PropertyNames) {
                    Console.Write(propName + ": ");
                    Console.WriteLine(props[propName][0]);
                }
            }
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ravi Vaddadi
Ravi Vaddadi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasonbrandt3
jasonbrandt3

ASKER

You're the man!
where would I put that?  What about searchng by whenCreated?