Following is the C# code needed to find the NetBIOS name of a domain from Active Directory:
using System;
using System.DirectoryServices;
void PrintNetBIOSName()
{
string netBIOSName = null;
DirectoryEntry rootDSE = new DirectoryEntry(
"LDAP://<server address>/rootDSE",
"<username>", "<password>");
string domain = (string)rootDSE.Properties[
"defaultNamingContext"][0];
if (!String.IsNullOrEmpty(domain))
{
DirectoryEntry parts = new DirectoryEntry(
"LDAP://<server address>/CN=Partitions
,CN=Configuration," + domain,
"<username>", "<password>");
foreach (DirectoryEntry part in parts.Children)
{
if ((string)part.Properties[
"nCName"][0] == domain)
{
netBIOSName = (string)part.Properties[
"NetBIOSName"][0];
break;
}
}
Console.WriteLine(netBIOSName);
}
}
REFERENCES:
3 Comments
September 24, 2007 at 3:19 am
is this a standard way to find that … i’m a noob in LDAP .. especially AD.
September 25, 2007 at 6:57 am
@Johnny: I guess so…
March 5, 2008 at 11:57 am
you can also use INameTranslate:
On any client, you can use the RootDSE object to retrieve the default naming context, which is the Distinguished Name of the domain that the user authenticated to. Then, you can use the NameTranslate object to convert this to the NetBIOS name of the domain. For example:
‘ Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
‘ Determine DNS name of domain from RootDSE.
Set objRootDSE = GetObject(“LDAP://RootDSE”)
strDNSDomain = objRootDSE.Get(“defaultNamingContext”)
‘ Use the NameTranslate object to find the NetBIOS domain name from the
‘ DNS domain name.
Set objTrans = CreateObject(“NameTranslate”)
objTrans.Init ADS_NAME_INITTYPE_GC, “”
objTrans.Set ADS_NAME_TYPE_1779, strDNSDomain
strNetBIOSDomain = objTrans.Get(ADS_NAME_TYPE_NT4)
‘ Remove trailing backslash.
strNetBIOSDomain = Left(strNetBIOSDomain, Len(strNetBIOSDomain) – 1)
(Extracted from http://www.rlmueller.net/NameTranslateFAQ.htm)