Thursday, June 02, 2005
One of the great things about .NET 2.0 is the new System.DirectoryServices.ActiveDirectory namespace. It contains a plethora of previously either obscure or difficult things to do using Ds* or NetApi* API calls. I was troubleshooting a trust relationship at a client and was digging how easy this was to do in 2.0:
public static void ShowAllTrusts()
{
Domain domain = Domain.GetCurrentDomain();
int i = 0;
foreach (TrustRelationshipInformation tri in domain.GetAllTrustRelationships())
{
Console.WriteLine("Trust #{0}", ++i);
Console.WriteLine("=============================================");
Console.WriteLine(
"Source Name: {0}",
tri.SourceName
);
Console.WriteLine(
"Target Name: {0}",
tri.TargetName
);
Console.WriteLine(
"Trust Direction: {0}",
tri.TrustDirection
);
Console.WriteLine(
"Trust Type: {0}",
tri.TrustType
);
string verifiedMsg = "true";
try
{
domain.VerifyTrustRelationship(
Domain.GetDomain(
new DirectoryContext(
DirectoryContextType.Domain,
tri.TargetName
)
),
tri.TrustDirection
);
}
catch (Exception ex)
{
verifiedMsg = ex.Message;
}
Console.WriteLine(
"Verified: {0}",
verifiedMsg
);
Console.WriteLine("=============================================");
Console.WriteLine();
}
}
That took all of 5 minutest to code and test using SnippetCompiler. Very cool.