如何使用System.DirectoryServices.Protocols连接到RootDSE和/或检索NetBiosDomain名称?

时间:2017-06-09 14:38:53

标签: active-directory ldap ldap-query

如果是目录条目,可以按如下方式连接并查找NetBios域名: -

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

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

但是,对于System.DirectoryServices.Protocols,如何在提供完全限定的域名时连接并查找此类NetBios域名?

1 个答案:

答案 0 :(得分:0)

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

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

            }

        }