Ldap: how to get a list of all domain names

I am new to LDAP. I am trying to list all NT domain names. By NT domain names, I mean the domain names that you find on the LAN. This can be observed on Windows XP machines when you try to log in to this computer (i.e., the Login dialog after pressing Ctrl + Alt + Del). Usually we select the domain name in the last drop-down list after entering the credentials.

I looked at this post and could not do anything. I don’t know what it is . The code provided in the message does this with rootdse. However, I have a specific server for request and request, and I consider it a domain controller. (Maybe I'm wrong). We are writing something like rootDSE

LDAP://<domain_name>/dc=<domain>,dc=org

As indicated in the post, I tried to find a property with a name rootDomainNamingContext. But I could not find him. Then I tried the following code:

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

I do not know what exactly to look for on the output that I received. I got the output:

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,

I really need a guide here.

UPDATE

I used the code below to get the domains:

    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

Now the problem is doubled. First, domain name inconsistencies. Suppose my DNS name is DC prod.domain.comand the expected domain names are for example dev, domain, etc. Instead, I get dev.domain.org, domain.org, etc. Some names that appear in the login window, when prompted, are displayed with a suffix domain.org; some have a suffix .org.

The second problem is not all domain names (which appear in the Windows login dialog, the third snapshot). I wonder why this is so.

UPDATE

, ( ) , dc .

+3
3

, , ! COM Interop.ActiveDs.dll(Active DS Type Library), , .

.

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/

+1

RootDSE LDAP clossary. DSE LDAP://<domain_name>/dc=<domain>,dc=org.

, , AD rootDomainNamingContext. , , , , . , , AD , , VB. , .

+2

@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";
        }
    }
0

Source: https://habr.com/ru/post/1757288/


All Articles