c#针对LDAP上的Active Directory

78l*_*lro 3 c# ldap active-directory

我正在编写针对Active Directory的一些c#,并且无休止地试图让它工作无济于事.以下代码有效,其后面的代码不起作用:

下面的代码使用"WinNT://"+ Environment.MachineName +",Computer"来建立连接并正常工作.

   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);
Run Code Online (Sandbox Code Playgroud)

我现在正试图改变这条线:

"WinNT://" + Environment.MachineName + ",Computer"
Run Code Online (Sandbox Code Playgroud)

"LDAP://MyDomainControllerName"
Run Code Online (Sandbox Code Playgroud)

但似乎无论我尝试什么价值取代价值'MyDomainControllerName'它都不会工作.

要获取'MyDomainControllerName'值,我右键单击MyComputer并按照其他地方的建议复制计算机名称值,但这不起作用.


当我尝试使用上面的LDAP:// RootDSE选项时,会导致以下错误:

位于路径LDAP:// RootDSE的Active Directory对象不是容器

这是你提到的成员方法的问题吗?

mar*_*c_s 7

是 - 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();
}
Run Code Online (Sandbox Code Playgroud)

或者省去麻烦并在C#源代码中抓住我的" Beavertail ADSI浏览器 " - 详细说明如何连接到RootDSE及其提供的内容.


Rob*_*ver 6

使用.NET Framework连接到AD时,可以使用"无服务器"绑定,也可以指定每次使用的服务器(服务器绑定).

以下是使用两者的示例:

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

// server bound
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://domainControllerName/dc=domainName,dc=com");
Run Code Online (Sandbox Code Playgroud)

我认为你误入歧途的地方是你忘了在你的域名中包含FQDN.希望这可以帮助.