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.
Comments [1] June 2, 2005 Trackback
This is the personal site of Ryan Dunn, co-author of the The .NET Developers Guide to Directory Services Programming.
Ryan currently works for Microsoft and is the Technical Evangelist for SQL Server Data Services (SSDS)
Buy the Book
Contact Ryan