3

我正在针对 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);

我现在正在尝试更改线路:

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

"LDAP://MyDomainControllerName"

但似乎无论我尝试什么值来代替值“MyDomainControllerName”,它都不会起作用。

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


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

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

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

4

5 回答 5

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();
}

或者省去麻烦,去拿我的 C# 源代码中的“ Beavertail ADSI 浏览器”——详细展示了如何连接到 RootDSE 及其提供的功能。

于 2009-01-31T22:18:16.200 回答
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");

我认为您误入歧途的地方是您最后忘记了包含您的域的 FQDN。希望这可以帮助。

于 2009-01-22T15:42:18.950 回答
0

您需要向它传递一个授权的用户名和密码。
尝试设置:DirectoryEntry.Username 和 DirectoryEntry.Password

于 2009-01-22T15:35:02.273 回答
0

您是否尝试过指定端口号和其他参数?

我们的 ldap 字符串如下所示:LDAP://myserver:1003/cn=admin@xyz.com|1,ou=Members,o=mdhfw2

于 2009-01-22T15:37:00.380 回答
0

看起来您需要获取 LDAP 连接信息。您可以调用 LDAP://RootDSE 来获取信息,如ASP.NET Wiki中所示。

请记住,LDAP 对象没有与 WINNT 对象相同的成员方法和属性,因此不要期望 group.Invoke("members") 和其他函数完全相同。您还应该阅读使用 LDAP的DirectoryServices 文档。

于 2009-01-22T16:26:27.787 回答