Search code examples
active-directoryldapldap-query

How does one connect to the RootDSE and/or retrieve NetBiosDomain Name with System.DirectoryServices.Protocols?


In case of Directory Entry, one can connect and find the NetBios Domain name as follows :-

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;
    }

where dnsDomainName is a Fully qualified Domain name .

However, in case of System.DirectoryServices.Protocols , How can one connect and find such NetBios Domain name when fully qualified domain name is given ?


Solution

  • Here is the solution i have got in one research paper:-

    private  string GetDomainNetBios(string sDomainFqdn,NetworkCredential netCred)
            {
                string sNetBios=string.Empty;
                LdapDirectoryIdentifier oLdapDirectory = null;
                LdapConnection oLdapConnection = null;
                try
                {
                    oLdapDirectory = new LdapDirectoryIdentifier(sDomainFqdn, 389);
                    oLdapConnection = (netCred == null)
                        ? new LdapConnection(oLdapDirectory)
                        : new LdapConnection(oLdapDirectory, netCred);
                    oLdapConnection.Timeout = TimeSpan.FromSeconds(45);
                    oLdapConnection.SessionOptions.TcpKeepAlive = true;
                    oLdapConnection.SessionOptions.ProtocolVersion = 3;
                    //prevents ldap connection from connecting to other servers during session
                    oLdapConnection.SessionOptions.ReferralChasing = ReferralChasingOptions.None;
                    oLdapConnection.AutoBind = false;
                    oLdapConnection.Bind();
                    SearchResponse dirRes = (SearchResponse)_ldapConnectionUsers.SendRequest(new
                        SearchRequest(
                            null,
                            "configurationNamingContext=*",
                            SearchScope.Base,
                            "configurationNamingContext"
                        ));
                    if (dirRes != null)
                    {
                        string sConfPartDn =
                            dirRes.Entries[0].Attributes["configurationNamingContext"][0].ToString();
                        dirRes = (SearchResponse)_ldapConnectionUsers.SendRequest(new SearchRequest(
                            sConfPartDn,
                            String.Format(CultureInfo.InvariantCulture,"(&(nETBIOSName=*)(dnsRoot={0}))", sDomainFqdn),
                            SearchScope.Subtree,
                            "nETBIOSName"
                        ));
                    }
    
                    if (dirRes != null && dirRes.Entries.Count > 0)
                    {
                        sNetBios = dirRes.Entries[0].Attributes["nETBIOSName"][0].ToString();
                    }
                    return sNetBios;
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format(CultureInfo.InvariantCulture,"{0}::{1}", new StackFrame(0,
                        true).GetMethod().Name, PvssMgrException.ToString(ex)));
                }
                finally
                {
                     oLdapConnection.Dispose();
    
                }
    
            }