Query Active Directory using LDAP

I need assistance using LDAP to Query Active Directory. I want to query all users and obtain their email address.

How do I do this using VB/C# .NET?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
Try this:

DirectoryEntry rootDSE = rootDSE = new DirectoryEntry("LDAP://OU="",OU=" ",dc="me",dc=com", username, password);

    DirectorySearcher search = new DirectorySearcher(rootDSE);

    search.PageSize = 1001;// To Pull up more than 100 records.

     search.Filter = "(&(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))";//UserAccountControl will only Include Non-Disabled Users.
      SearchResultCollection result = search.FindAll();

         foreach (SearchResult item in result)
            if (item.Properties["cn"].Count > 0)
                DisplayName = item.Properties["cn"][0].ToString();
            if (item.Properties["mail"].Count > 0)
                EmailAddress = item.Properties["mail"][0].ToString();
            if (item.Properties["SamAccountName"].Count > 0)
                DomainName = item.Properties["SamAccountName"][0].ToString();
            if (item.Properties["department"].Count > 0)
                Department = item.Properties["department"][0].ToString();
            if (item.Properties["title"].Count > 0)
                title = item.Properties["title"][0].ToString();
            if (item.Properties["company"].Count > 0)
                company = item.Properties["company"][0].ToString();
            if (item.Properties["DistinguishedName"].Count > 0)
                memberof = item.Properties["DistinguishedName"][0].ToString();
            if (item.Properties["AccountExpirationDate"].Count > 0)
                string aaa = item.Properties["AccountExpirationDate"][0].ToString();

              dt.Rows.Add(DisplayName, EmailAddress, DomainName, Department, title, company, memberof);
             DisplayName = string.Empty;
             EmailAddress = string.Empty;
             DomainName = string.Empty;
             Department = string.Empty;
             title = string.Empty;
             company = string.Empty;
             memberof = string.Empty;


Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Walter RitzelSenior Software EngineerCommented:
HLRosenbergerAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.