Where is the domain name in a UserPrincipal object?
.net, active-directory, directoryservices
Solution
For AD DS, the value of `msDS-PrincipalName` is the NetBIOS domain name, followed by a backslash ("\").
You can find it using :
/* Retreiving the root domain attributes
*/
sFromWhere = "LDAP://DC_DNS_NAME:389/dc=dom,dc=fr";
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD");
DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase);
dsLookForDomain.Filter = "(objectClass=*)";
dsLookForDomain.SearchScope = SearchScope.base;
dsLookForDomain.PropertiesToLoad.Add("msDS-PrincipalName");
SearchResult srcDomains = dsLookForDomain.FindOne();
Problem
I'm using the `System.DirectoryServices.ActiveDirectory` classes to find all Active Directory users. The code is very simple: ``` var context = new PrincipalContext(ContextType.Domain); var searcher = new PrincipalSearcher(new UserPrincipal(context)); var results = searcher.FindAll(); ``` I want to get the domain-qualified username in the "friendly" (aka. "pre-Windows 2000" format), eg. "CONTOSO\SmithJ". `UserPrincipal.SamAccountName` gives me the username part, but how do I get the domain part? I cannot assume that the domain will be the same as the machine's or current user's domain.