Max Schmeling
Max Schmeling

Reputation: 12509

How to get short "domain name" from dns domain name?

Forgive me if my understanding of this topic has some shortcomings, I only know what I know about domains and active directory because of what I've picked up from working with them.

There are two different "versions" of a domain name. The first is what I call the DNS domain name which would be like company.int (for the user [email protected]) and the second would be like prefixname (for the user prefixname\max) and they would both refer to the same thing.

My question is, given "company.int", how do I convert that to "prefixname"?

EDIT: Or given a System.DirectoryServices.ActiveDirectory.Domain object, how do I get the prefixname?

EDIT2: Also, is there a name for the "prefixname" other than that? I never know what to call it.

EDIT3: The value I'm trying to get is the same value that shows up on the windows login screen for "Log on to" (where it lists the domains and your computer).

EDIT4: I've figured out I can get the value by doing the following:

SecurityIdentifier sid = GetCurrentUserSID();
string prefixName = sid.Translate(typeof(NTAccount)).Value.Split('\\')[0];

Does anyone know of a better method to get this name?

Upvotes: 2

Views: 10534

Answers (3)

marc_s
marc_s

Reputation: 754478

This should do it, I hope:

    private string GetNetbiosDomainName(string dnsDomainName)
    {
        string netbiosDomainName = string.Empty;

        DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");

        string configurationNamingContext = rootDSE.Properties["configurationNamingContext"][0].ToString();

        DirectoryEntry searchRoot = new DirectoryEntry("LDAP://cn=Partitions," + configurationNamingContext);

        DirectorySearcher searcher = new DirectorySearcher(searchRoot);
        searcher.SearchScope = SearchScope.OneLevel;
        searcher.PropertiesToLoad.Add("netbiosname");
        searcher.Filter = string.Format("(&(objectcategory=Crossref)(dnsRoot={0})(netBIOSName=*))", dnsDomainName);

        SearchResult result = searcher.FindOne();

        if (result != null)
        {
            netbiosDomainName = result.Properties["netbiosname"][0].ToString();
        }

        return netbiosDomainName;
    }

You basically call it with the "mydomain.com" and should get back the netbios domain name, e.g. "MYDOMAIN" (usually).

Marc

Upvotes: 4

Russ Bradberry
Russ Bradberry

Reputation: 10865

are you looking for System.DirectoryServices.DirectoryEntry.Path?

and the prefixname is just called domain

EDIT: what about Environment.UserDomainName?

Upvotes: 0

Martin v. Löwis
Martin v. Löwis

Reputation: 127457

To my knowledge, prefixname is always the first label of company.int (i.e. company).

Upvotes: 0

Related Questions