views:

460

answers:

3

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?

A: 

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

Martin v. Löwis
It is not the same where I work.
Max Schmeling
A: 

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

and the prefixname is just called domain

EDIT: what about Environment.UserDomainName?

Russ Bradberry
No. Just added more clarification to the question btw.
Max Schmeling
edited to add another possibility
Russ Bradberry
Environment.UserDomainName is the value I want, but I suspect that it's getting that value for the user executing the application, when I need the value for the user of the application (that's different when considering web apps) or for any arbitrary dns domain name.
Max Schmeling
A: 

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

marc_s
I've accepted your answer because you put effort into it and it is technically correct, though I've gone with a different method. Looking at the code for Environment.UserDomainName convinced me that what I had already done (Account.Value.Split('\\')[0]) is the way to go. If you disagree please let me know why.
Max Schmeling
THanks, Max! I would believe that environment variable holds the right domain name - unless it has been cleared for some reason. Relying on the old "DOS" environment is a bit shaky at times - things can be cleared and removed - the AD containers I'm querying will always be there and will always hold the real value you're looking for.
marc_s