Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get list of all domains in Active Directory using C#

Can anyone please help me to get all the domains in Active Directory. I have tried many times, but all the programs are listing only the current working domain.

How can I do this?

like image 729
Arun Avatar asked Nov 27 '08 12:11

Arun


People also ask

How do I list all domain controllers?

To list all domain controllers the Get-ADDomainController PowerShell cmdlet is used. The Get-ADDomainController cmdlet can get all domain controllers or list specific ones with the various search parameters.

How do you list all domains in a forest?

The traditional approach to finding and listing the Domain Controllers(DCs) in a forest is to use the Get-ADDomainController PowerShell command.

How do I find Active Directory forest?

Right-click the root domain, and click Properties. Under the General tab, you will find the forest and domain functional levels currently configured on your Active Directory Domain Controller.


2 Answers

I had some issues getting LeeMobile's code to work in my case bacause it was trying to find my application's current domain context while running forest.Domains. I was able to get around it by doing something like this.

Forest forest = Forest.GetForest(new DirectoryContext(DirectoryContextType.Forest, "yourForestDomain", "username", "password"));
DomainCollection domains = forest.Domains;
like image 116
Paul Avatar answered Sep 27 '22 17:09

Paul


Domain domain = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, "yourDomain", "username", "password"));

Forest forest = domain.Forest;

DomainCollection domains = forest.Domains;

The above uses the System.DirectoryServices.ActiveDirectory namespace. It'll give you a domain collection containing all the domains that are in the same forest as your given domain.

like image 42
LeeMobile Avatar answered Sep 27 '22 15:09

LeeMobile