views:

70

answers:

3

Hi,

I have the DN of a user in Active Directory, I want to get the "NT style" domain\user from this. The sAMAccountname AD property gives me the user part, but what about the domain?

Thanks

A: 

How about using --> System.Security.Principal.NTAccount.ToString()

See msdn info about it here: NTAccount.ToString()

This should return a string in the format of domain\user... is this what you are after?

Xander
A: 

You can get it by taking the last part of the user DN (DC=domain,DC=local) and adding CN=Partitions,CN=Configuration, before.

Then do a subtree search for (&(nCName="DC=domain,DC=local")(nETBIOSName=*)) with CN=Partitions, CN=Configuration, DC=domain, DC=local as the starting point; the entry you get back will have the NETBIOS name of the domain in the nETBIOSName-attribute.

Per Noalt
Thanks, that looks good. However, I am guaranteed to only get ONE value for this search?
Henrik
If you have multiple domains in your forrest, you'll need to include the domain you're after in your search; I've modified my reply to take this into account.
Per Noalt
A: 

The easiest way to do this conversion is through the DsCrackNames API. You specify the input format and output format and it does the conversion for you.

Michael Thorp