简体   繁体   English

c#针对LDAP上的Active Directory

[英]c# against Active Directory over LDAP

I'm coding some c# against Active Directory and have tried endlessly to get this to work to no avail. 我正在编写针对Active Directory的一些c#,并且无休止地试图让它工作无济于事。 The following code works and the code that follows it does not: 以下代码有效,其后面的代码不起作用:

The code below is using "WinNT://" + Environment.MachineName + ",Computer" to make the connection and works fine. 下面的代码使用“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);

I'm now trying to change the line: 我现在正试图改变这条线:

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

to

"LDAP://MyDomainControllerName"

but it seems no matter what value I try in place of the value 'MyDomainControllerName' it wont work. 但似乎无论我尝试什么价值取代价值'MyDomainControllerName'它都不会工作。

To get the 'MyDomainControllerName' value I right clicked on MyComputer and copied the computer name value as suggested elsewhere but this didn't work. 要获取'MyDomainControllerName'值,我右键单击MyComputer并按照其他地方的建议复制计算机名称值,但这不起作用。


When I try using the LDAP://RootDSE option above it results in the following error: 当我尝试使用上面的LDAP:// RootDSE选项时,会导致以下错误:

The Active Directory object located at the path LDAP://RootDSE is not a container 位于路径LDAP:// RootDSE的Active Directory对象不是容器

Is this a problem with the member methods as you mention? 这是你提到的成员方法的问题吗?

Yes- RootDSE is not a container - but it holds a number of interesting properties which you can query for - eg the name of your domain controller(s). 是 - RootDSE不是容器 - 但它包含许多您可以查询的有趣属性 - 例如域控制器的名称。

You can check these out by using code like this: 您可以使用以下代码检查这些:

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

Or save yourself the trouble and go grab my " Beavertail ADSI Browser " in C# source code - shows in detail how to connect to RootDSE and what it offers. 或者省去麻烦并在C#源代码中抓住我的“ Beavertail ADSI浏览器 ” - 详细说明如何连接到RootDSE及其提供的内容。

When connecting to AD using the .NET Framework, you can use "serverless" binding or you can specify a server to use everytime (server bound). 使用.NET Framework连接到AD时,可以使用“无服务器”绑定,也可以指定每次使用的服务器(服务器绑定)。

Here's an example of using both: 以下是使用两者的示例:

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

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

I think where you were going astray is you forgot to include the FQDN for your domain on the end. 我认为你误入歧途的地方是你忘了在你的域名中包含FQDN。 Hope this helps. 希望这可以帮助。

You need to pass it an authorized Username and password. 您需要传递一个授权的用户名和密码。
try setting: DirectoryEntry.Username and DirectoryEntry.Password 尝试设置:DirectoryEntry.Username和DirectoryEntry.Password

have you tried speciying the port number and other parms? 您是否尝试过指定端口号和其他参数?

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

It looks like you need to get the LDAP connection information. 看起来您需要获取LDAP连接信息。 You can call LDAP://RootDSE to get the information as shown in the ASP.NET Wiki . 您可以调用LDAP:// RootDSE来获取ASP.NET Wiki中显示的信息

Please keep in mind that the LDAP objects do not have the same member methods and properties as the WINNT objects, so do not expect the group.Invoke("members") and other functions to work exactly the same. 请记住,LDAP对象没有与WINNT对象相同的成员方法和属性,因此不要指望group.Invoke(“members”)和其他函数完全相同。 You should read up on the DirectoryServices documentation with LDAP as well. 您还应该使用LDAP阅读DirectoryServices文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM