2009-01-22 103 views
3

我正在编写一些针对Active Directory的c#并试图无休止地让这个工作无效。下面的代码工作,并遵循它的代码不:c#针对通过LDAP的Active Directory

下面的代码使用“WinNT://”+ Environment.MachineName +“,计算机”进行连接并工作正常。

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的Active Directory对象:// RootDSE的不是一个容器

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

+0

我不完全理解你的问题根据你的代码示例。您是否试图使用LDAP来枚举本地组的成员身份?如果是这样,那就行不通了。 – barneytron 2009-01-23 03:45:24

回答

6

当连接到AD使用.NET Framework,你可以使用“无服务器”绑定,或者您可以指定每次使用的服务器(服务器绑定)。

下面是同时使用的例子:

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

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

我想,你误入歧途打算是你忘了,包括在FQDN上月底域。希望这可以帮助。

+0

当我右键点击我的电脑,看看电脑名称,它似乎是mypc.domain.net - 所以我尝试LDAP://dc =域,dc = net,我试过LDAP:// mypc/dc =域,dc = net和两个我得到一个错误,告诉我一个无效的dn语法已被指定。 一切顺利 – 78lro 2009-01-26 10:38:21

0

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

0

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

我们的LDAP字符串看起来像:LDAP:// MYSERVER:1003/[email protected] | 1,OU =成员,O = mdhfw2

0

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

请记住,LDAP对象没有与WINNT对象相同的成员方法和属性,所以不要指望group.Invoke(“members”)和其他函数完全相同。您还应该阅读使用LDAP的DirectoryServices documentation

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

或保存自己的麻烦,去抓住我的在C“Beavertail ADSI Browser”#源代码 - 详细显示了如何连接到的RootDSE和它所提供。

相关问题