Question

Using System.DirectoryServices (i.e. the DirectoryEntry class) is there a way to convert the DistinguishedName attribute to, or get the Canonical Name that would normally appear in the Active Directory Users & Computers snap-in? I realize I could do this using regular expressions, but I would prefer a more reliable approach.

For example, I want to convert this

CN=Murdock\, James,OU=Disabled Users,OU=GOG,DC=contoso,DC=local

to this

contoso.local/GOG/Disabled Users/Murdock, James

Was it helpful?

Solution

It's a constructed attribute. On a DirectoryEntry, you need to use RefreshCache:

var de = new DirectoryEntry("CN=Murdock\, James,OU=Disabled Users,OU=GOG,DC=contoso,DC=local");
de.RefreshCache(new string[] {"canonicalName"});

OTHER TIPS

You'll want the canonicalName attribute. Assuming you already have the code for getting the user as a DirectoryEntry.

userDe.Properties["canonicalName"][0].ToString()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top