【问题标题】:c# against Active Directory over LDAPc# 通过 LDAP 针对 Active Directory
【发布时间】:2009-01-22 15:30:22
【问题描述】:

我正在针对 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 对象不是容器

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

【问题讨论】:

  • 根据您的代码示例,我不完全理解您的问题。您是否尝试使用 LDAP 枚举本地组的成员资格?如果是这样,那就行不通了。

标签: c# active-directory ldap


【解决方案1】:

是的 - 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 Browser” - 详细展示了如何连接到 RootDSE 及其提供的功能。

【讨论】:

    【解决方案2】:

    使用 .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。希望这会有所帮助。

    【讨论】:

    • 当我右键单击我的计算机并查看计算机名称时,它似乎是 mypc.domain.net - 所以我尝试了 LDAP://dc=domain,dc=net 并尝试了 LDAP ://mypc/dc=domain,dc=net 和我都收到一个错误,告诉我指定了无效的 dn 语法。万事如意
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        您似乎需要获取 LDAP 连接信息。您可以调用LDAP://RootDSE获取ASP.NET Wiki所示信息。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-01-26
          • 2011-11-05
          • 1970-01-01
          • 1970-01-01
          • 2015-09-11
          • 2010-09-26
          • 1970-01-01
          相关资源
          最近更新 更多