When we log into our Windows machines at work, we use “CORP\username”.
Given a user’s SID and the domain controller where they logged in, how do I derive that string? I’m using the DirectoryServices APIs added in .NET 3.5, like this:
PrincipalContext domaincontroller = new PrincipalContext(ContextType.Domain, "192.168.30.115");
UserPrincipal user = UserPrincipal.FindByIdentity(domaincontroller, IdentityType.Sid, "S-1-5-21-293182769-1777760488-2957165303-1798");
I’ve dug around the domaincontroller object and user object, and see lots of stuff like this:
user.Name: john smith
user.DisplayName: john smith
user.UserPrincipalName: john.smith@corp.mycompany.com
user.SamAccountName: john.smith
If I dig deep in the private guts of the UserPrinciple object, I find two fields labelled “domainFlatName” and “FlatDomainName” which contain exactly what I want (screen shot below). What do these mean, and how I can get to them via a public interface?

Getting this value is unfortunately non-trivial. The attribute you want is
nETBIOSNamewhich is stored on thecrossRefobject representing the domain in the configuration NC.Given you know the DN of the domain (e.g. fabrikam.com is DC=fabrikam,DC=com), you can do a search of the configuration naming context for
(&(objectClass=crossRef)(nCName=DC=fabrikam,DC=com))and grab thenETBIOSNameoff of there.