Get all users from AD domain

2019-01-20 13:53发布

问题:

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!

回答1:

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.



回答2:

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



回答3:

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
}