C # vs Active Directory via LDAP

I am coding some C # in Active Directory and endlessly trying to get this to work to no avail. The following code works, and the following code does not follow:

The code below uses "WinNT: //" + Environment.MachineName + "computer" to make the connection and work fine.

   DirectoryEntry localMachine = new DirectoryEntry
        ("WinNT://" + Environment.MachineName + ",Computer");

    DirectoryEntry admGroup = localMachine.Children.Find
        ("Administrators", "group");

    object members = admGroup.Invoke("members", null);

    foreach (object groupMember in (IEnumerable)members)
    {
        DirectoryEntry member = new DirectoryEntry(groupMember);
        output.RenderBeginTag("p");
        output.Write(member.Name.ToString());
        output.RenderBeginTag("p");
    }



    base.Render(output);

Now I am trying to change the line:

"WinNT://" + Environment.MachineName + ",Computer"

to

"LDAP://MyDomainControllerName"

but it seems that no matter what value I try instead of the value "MyDomainControllerName", it does not work.

To get the value "MyDomainControllerName", I right-clicked on MyComputer and copied the value of the computer name, as suggested elsewhere, but this did not work.


When I try to use the LDAP: // RootDSE option above, this results in the following error:

Active Directory, LDAP://RootDSE

-, ?

+3
5

AD .NET Framework "serverless" , ( ).

:

// serverless
DirectoryEntry rootConfig = new DirectoryEntry("LDAP://dc=domainname,dc=com");

// server bound
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://domainControllerName/dc=domainName,dc=com");

, - . , .

+6

. RootDSE , , - . .

, :

DirectoryEntry deRoot = new DirectoryEntry("LDAP://RootDSE");

if (deRoot != null)
{
  Console.WriteLine("Default naming context: " + deRoot.Properties["defaultNamingContext"].Value);
  Console.WriteLine("Server name: " + deRoot.Properties["serverName"].Value);
  Console.WriteLine("DNS host name: " + deRoot.Properties["dnsHostName"].Value);

  Console.WriteLine();
  Console.WriteLine("Additional properties:");
  foreach (string propName in deRoot.Properties.PropertyNames)
    Console.Write(propName + ", ");
  Console.WriteLine();
}

" Beavertail ADSI Browser" # - , RootDSE .

+6

.
: DirectoryEntry.Username DirectoryEntry.Password

0

?

ldap : LDAP://myserver: 1003/cn=admin@xyz.com | 1, ou = Members, o = mdhfw2

0

, LDAP. LDAP://RootDSE, , ASP.NET Wiki.

Keep in mind that LDAP objects do not have the same member methods and properties, such as WINNT objects, so do not expect the .Invoke group ("members") and other functions to work the same way. You should read the DirectoryServices Documentation with LDAP.

0
source

All Articles