如何连接到 RootDSE and/or 使用 System.DirectoryServices.Protocols 检索 NetBios 域名?

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

在目录条目的情况下,可以连接并找到 NetBios 域名,如下所示:-

私有字符串 GetNetbiosDomainName(字符串 dnsDomainName) { 字符串 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;
    }

其中 dnsDomainName 是一个完全限定的域名。

但是,在 System.DirectoryServices.Protocols 的情况下,如何在给出完全限定域名的情况下连接并找到这样的 NetBios 域名?

这是我在一篇研究论文中得到的解决方案:-

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();

            }

        }