Welcome to Christoffer Andersson Sign in | Join | Help

Enum all domains from the current forest?

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;

Published den 6 januari 2008 17:21 by Christoffer Andersson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
required 
(required)