How to get a "flat" domain name for a user in .NET 3.5+

When we log into our Windows computers, we use "CORP \ username".

Given the SID of the user and the domain controller where they logged in, how can I get this line? I am using DirectoryServices APIs added in .NET 3.5, for example:

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 dug up the domaincontroller object and the user object and saw a lot of things like this:

user.Name:              john smith
user.DisplayName:       john smith
user.UserPrincipalName: john.smith@corp.mycompany.com
user.SamAccountName:    john.smith

If I delve into the private brushes of the UserPrinciple object, I find two fields labeled "domainFlatName" and "FlatDomainName" that contain exactly what I want (screenshot below). What does this mean and how can I get to them through an open interface?

Object Inspector Screenshot

+3
source
3

, , . , , nETBIOSName, crossRef, NC.

, DN (, fabrikam.com DC = fabrikam, DC = com), (&(objectClass=crossRef)(nCName=DC=fabrikam,DC=com)) nETBIOSName.

0

, . , domainFlatName/FlatDomainName NETBIOS ( NETBIOS DNS).

, ? , Active Directory. DsGetDcName DS_RETURN_FLAT_NAME. , .NET, , .

, "CORP\username". , System.Security.Principal.WindowsIdentity.GetCurrent().Name. SecurityIdentifer Translate NTAccount Value. , LookupAccountName DsCrackNames . , Active Directory, msDS-PrincipalName.

+1

. , , - SamAccountName UserPrincipal.Current

DirectorySearcher SearchRoot.Name, , "DC = CORP"

   string sFilter = String.Format("(&(objectClass=user)(objectCategory=person)(sAMAccountName={0}))", u.SamAccountName);
                DirectoryEntry de = new DirectoryEntry(adPath);
                DirectorySearcher ds = new DirectorySearcher(de, sFilter, new string[] { "distinguishedName" });
string dcName = ds.SearchRoot.Name;

Extracting CORP from DC = CORP and combining it with u.SamAccountName was the best way to find

It will be interesting to know if there is still a direct way to get flatdomainname

0
source

All Articles