TerrorAustralis
TerrorAustralis

Reputation: 2923

Get all users from AD domain

I have a need to be able to identify all of the users on my AD (Active Directory) domain. I have the domain name and that is about it. It would rock if i could get it as a list of UserPrincipal or something but if its just a string then i can get the rest of the info i need from there.

Thanks!

Upvotes: 2

Views: 18200

Answers (3)

Michael Pakhantsov
Michael Pakhantsov

Reputation: 25390

look to this article: How to: (Almost) Everything In Active Directory via C#

Upvotes: 5

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

If you just have to get the users list you can use this code -

var dirEntry = new DirectoryEntry(string.Format("LDAP://{0}/{1}", "x.y.com", "DC=x,DC=y,DC=com"));
var searcher = new DirectorySearcher(dirEntry)
         {
             Filter = "(&(&(objectClass=user)(objectClass=person)))"
         };
var resultCollection = searcher.FindAll();

However if you have to more operations with AD you should consider using LINQ to AD API http://linqtoad.codeplex.com/

It is a Linq based API to work with AD. Easy to use and I have got some good results with it.

Upvotes: 7

Johann Blais
Johann Blais

Reputation: 9469

I think you can use something like that:

DirectoryEntry domain = new DirectoryEntry("LDAP://domain.com/CN=Users,DC=Domain,DC=com");
foreach (DirectoryEntry child in domain.Children)
{
    // code
}

Upvotes: 2

Related Questions