【问题标题】:How to get domain alias using System.DirectoryServices.ActiveDirectory.Domain class如何使用 System.DirectoryServices.ActiveDirectory.Domain 类获取域别名
【发布时间】:2012-12-11 05:51:43
【问题描述】:

我们有一个全名的域,例如long-domainname.com;此域名将替换为别名 short。可以使用netapi32.dll 检索此别名,如下所示:

[DllImport("Netapi32.dll")]
static extern int NetApiBufferFree(IntPtr Buffer);

// Returns the domain name the computer is joined to, or "" if not joined.
public static string GetJoinedDomain()
{
    int result = 0;
    string domain = null;
    IntPtr pDomain = IntPtr.Zero;
    NetJoinStatus status = NetJoinStatus.NetSetupUnknownStatus;
    try
    {
        result = NetGetJoinInformation(null, out pDomain, out status);
        if (result == ErrorSuccess &&
            status == NetJoinStatus.NetSetupDomainName)
        {
            domain = Marshal.PtrToStringAuto(pDomain);
        }
    }
    finally
    {
        if (pDomain != IntPtr.Zero) NetApiBufferFree(pDomain);
    }
    if (domain == null) domain = "";
    return domain;
}

此方法返回 sort 值。但是使用System.DirectoryServices.ActiveDirectory.Domain 类及其Name 属性,我得到了 long-domainname.com 值。在 Debug 模式下搜索属性,我找不到任何 short 值字段或属性。 System.DirectoryServices.ActiveDirectory.Domain 类可以吗?或者可能有一些其他类的System.DirectoryServices 命名空间?如何在不导入外部*.dll的情况下获取域名值?

【问题讨论】:

    标签: c# active-directory alias domain-name


    【解决方案1】:
    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;
        }
    

    【讨论】:

    • 我赞成这个,因为我觉得它非常有用。我将 DirectoryEntry 行更改为: DirectoryEntry rootDSE = new DirectoryEntry(string.Format("LDAP://{0}/RootDSE",dnsDomainName));这样您就可以获得其他(受信任的)林中域的 NETBIOS 域名。
    • Daro 很好,我们都在这里互相帮助,互相学习.. 不客气
    猜你喜欢
    • 2015-11-06
    • 2018-12-28
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多