ldap:如何获取所有域名的列表

时间:2010-07-30 07:37:55

标签: .net active-directory domain-name

我是LDAP的新手。我试图列出所有NT域名。通过NT域名我的意思是,您将在LAN网络上找到的域名。当您尝试登录到该计算机时(即按下ctrl + alt + del后得到的登录对话框),您可以在Windows XP计算机上观察到这一点。通常我们在输入凭据后在最后一个下拉列表中选择域名。

我已经看过 this post ,并且无法做出任何改变。我不知道rootDSE是什么。帖子中给出的代码用rootdse做。但是,我有一个特定的服务器来命中和查询,我认为它是一个域控制器。 (我可能错了)。我们写了像

这样的东西
LDAP://<domain_name>/dc=<domain>,dc=org

正如在帖子中给出的,我试图寻找名为rootDomainNamingContext的属性。但我找不到它。然后我尝试了以下代码:

Sub Main()
        Dim oRoot As DirectoryEntry = Nothing
        'Dim oSearcher As DirectorySearcher
        'Dim oResults As SearchResultCollection

        Try

            oRoot = New DirectoryEntry("LDAP://<domain_name>/dc=<domain>,dc=org")
            For Each obj As String In oRoot.Properties.PropertyNames
                Console.Write(obj + ", ")
            Next
        Catch ex As Exception
            Console.Write(ex.Message)
        Finally
            oRoot.Dispose()
        End Try

        Console.Read()
    End Sub

我不知道在我得到的输出中要具体查找什么。我得到的输出是:

objectClass, description, distinguishedName, instanceType, whenCreated, whenChan
ged, subRefs, uSNCreated, dSASignature, repsTo, repsFrom, uSNChanged, name, obje
ctGUID, replUpToDateVector, creationTime, forceLogoff, lockoutDuration, lockOutO
bservationWindow, lockoutThreshold, maxPwdAge, minPwdAge, minPwdLength, modified
CountAtLastProm, nextRid, pwdProperties, pwdHistoryLength, objectSid, uASCompat,
 modifiedCount, auditingPolicy, nTMixedDomain, rIDManagerReference, fSMORoleOwne
r, systemFlags, wellKnownObjects, objectCategory, isCriticalSystemObject, gPLink
, gPOptions, masteredBy, ms-DS-MachineAccountQuota, msDS-Behavior-Version, msDS-
PerUserTrustQuota, msDS-AllUsersTrustQuota, msDS-PerUserTrustTombstonesQuota, ms
Ds-masteredBy, dc, nTSecurityDescriptor,

我真的需要这里的指导。

更新

我已使用以下代码获取域名:

    Dim dc As New DirectoryContext(DirectoryContextType.DirectoryServer, DcIpAddr)
    Dim domc As DomainController = DomainController.GetDomainController(dc)
    For Each dmn As Domain In domc.Forest.Domains
        Console.WriteLine(dmn.Name)
    Next

现在这个问题有两个问题。首先,域名不一致。假设我的DC的DNS名称为prod.domain.com,并且预期的域名域名用于例如dev, domain, etc。相反,我得到dev.domain.org, domain.org, etc。查询时出现在窗口登录对话框中的某些名称带有后缀domain.org;有些人有后缀.org

第二个问题并非出现所有域名(出现在Windows登录对话框中,第3个下拉列表)。不知道为什么会这样?

更新

图中其​​他域(未显示)可能是另一个域控制器服务器的一部分,或者我需要使用适当的凭据访问dc。

3 个答案:

答案 0 :(得分:2)

RootDSE在LDAP clossary中定义。在您的情况下,根DSE可以是LDAP://<domain_name>/dc=<domain>,dc=org

如果您的AD服务器已定义rootDomainNamingContext,您应该能够使用其他Stack Overflow问题上的代码获取域。由于您似乎没有该属性,因此您需要迭代记录以找到适当的属性。我建议您通过物理或远程桌面访问域控制器,并打开AD目录并查找条目及其属性,以便从VB查询所需内容。如果您无法访问,请询问您的系统管理员。

答案 1 :(得分:1)

看起来我为你找到了一个解决方案! 我尝试为Interop.ActiveDs.dll(Active DS Type Library)导入COM参考,然后我们可以将这些名称翻译成您喜欢的名称。

这是我的代码。

using System.DirectoryServices.ActiveDirectory;
using ActiveDs;

private void ListDomains()
{
    string sUserName = "xxxx";
    string sPassword = "xxxx";

    DirectoryContext oDirectoryContext = new DirectoryContext(DirectoryContextType.Domain, sUserName, sPassword);

    Domain oCurrentDomain = Domain.GetDomain(oDirectoryContext);
    Forest oForest = oCurrentDomain.Forest;
    DomainCollection oAddDomainsInForest = oForest.Domains;

    foreach (Domain oDomain in oAddDomainsInForest)
    {
        Console.WriteLine(GetFriendlyName(oDomain.ToString()));
    }           
}

private string GetFriendlyName(string sDomainName)
{
    try
    {
        IADsADSystemInfo oSysInfo = new ADSystemInfoClass();
        IADsNameTranslate oNameTranslate = new NameTranslateClass();
        oNameTranslate.Init((int)ADS_NAME_INITTYPE_ENUM.ADS_NAME_INITTYPE_DOMAIN, sDomainName);

        string[] aSplitDN = sDomainName.Split(new Char[] { '.' });
        string sDistinguishedName = "";

        //Convert Domain Name to Distinguished Name
        foreach (string sDomainPart in aSplitDN)
        {
            sDistinguishedName = sDistinguishedName + "DC=" + sDomainPart + ",";
        }

        oNameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_UNKNOWN, sDistinguishedName.Remove(sDistinguishedName.Length - 1));//Remove the last comma
        string sFriendlyName = oNameTranslate.Get((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4);
        return sFriendlyName(@"\", "");
    }
    catch
    {
        return "Access Denied";
    }
}

有关详细说明,请点击此处查看我的文章 http://anyrest.wordpress.com/2010/08/06/how-to-get-domain-name-pre-windows-2000-from-fqdn-fully-qualified-domain-name-using-c/

答案 2 :(得分:0)

@ Raymund的代码使用了一点LINQ:

    private static string GetFriendlyName(string names)
    {
        try
        {
            string[] arr = names.Split('.');
            //Convert Domain Name to Distinguished Name
            string distinguishedName = String.Join(",", arr.Select(d => "DC=" + d));

            IADsADSystemInfo info = new ADSystemInfo();
            IADsNameTranslate nameTranslate = new NameTranslate();
            nameTranslate.Init((int)ADS_NAME_INITTYPE_ENUM.ADS_NAME_INITTYPE_DOMAIN, names);
            nameTranslate.Set((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_UNKNOWN, distinguishedName);

            string friendlyName = nameTranslate.Get((int)ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_DOMAIN_SIMPLE);
            return friendlyName.Replace("\\", String.Empty);
        }
        catch
        {
            return "Access Denied";
        }
    }