Enum all domains from the current forest?

Published 06 januari 08 05:21 | Christoffer Andersson 
Happy New Year!
I got some time to work at the Fine Grain Password Policy tool, it looks like beta 2 is going to be out very soon, the beta 1 release did only support working with one domain (the current domain), the beta 2 release can connect to any domain, as well the connect dialog will enum all domains it can find based on the current forest, as well trusted domains. Here is how i do that using the DirectoryServices.ActiveDirectory in Microsoft .NET Framework 2.0: (the code will look wired, it's a copy past directly from the DirectoryServices functions lib for FGPP).

public static List<string> GetADDomainsFromCurrentForest()

{

List<string> domains = new List<string>();

foreach (Domain domain in Forest.GetCurrentForest().Domains)

{

try

{

domains.Add(domain.Name);

}

catch (Exception e)

{

//

}

}

foreach (TrustRelationshipInformation trust in Forest.GetCurrentForest().GetAllTrustRelationships())

{

try

{

DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, trust.TargetName);

domains.Add(Domain.GetDomain(context).Name);

}

catch (Exception e)

{

//

}

}

return domains;

Comments

No Comments
Anonymous comments are disabled