为什么我的DirectoryEntry突然停止工作?

问题描述:

我一直在使用DirectoryServices命名空间中的DirectoryEntry对象,它一直很好。但是我修改了其中一个LDAP类,并突然停止了32位版本的程序工作。

我将ActiveDs引用导入到我的项目中,以便将ADSI对象转换为适当的类型。但从那时起,我的项目在创建DirectoryEntry对象实例时无法正确绑定。


我已经尝试绑定与LDAP和WinNT提供程序,但无济于事。我得到的错误是0x80005000未知错误,因为它试图绑定。

即使这样简单的代码失败(这并不奇怪,因为绑定是一个重要组成部分!)
为什么我的DirectoryEntry突然停止工作?

static void Main(string[] args) 
{ 
    try 
    { 
     using(var de = new DirectoryEntry("LDAP://servername", "user", "password", AuthenticationType.Secure) 
     { 
      Console.WriteLine(de.Name) 
     } 
    } 
    catch(Exception ex) 
    { 
     Console.WriteLine(ex) 
    } 
} 

是否有任何理由为什么这应该突然停止工作?一个引用是否可以破坏32位机器上已有的DLL?

注意:
我也尝试使用VBS脚本查询LDAP,但它只是挂起。从达罗的建议


结果:
代码

static void Main() 
{ 
    try 
    { 
     using (var ldap = new LdapConnection(new LdapDirectoryIdentifier("ad1"))) 
     { 
      ldap.Bind(); 
      using (var de = new DirectoryEntry("LDAP://" + ldap.SessionOptions.DomainName)) 
      { 
       Console.WriteLine(de.Name); 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     using(var fs = new FileStream("C:\\error.log", FileMode.Create, FileAccess.Write)) 
     { 
      using(var sw = new StreamWriter(fs)) 
      { 
       sw.WriteLine(ex.Message); 
       sw.WriteLine(ex.StackTrace); 
      } 
     } 
    } 



日志文件产生:
未知错误(0x80005000)
在System.Di rectoryServices.DirectoryEntry.Bind(布尔throwIfFail)
在System.DirectoryServices.DirectoryEntry.Bind()
在System.DirectoryServices.DirectoryEntry.get_Name()
在Test.Program.Main()在C:\ Users \用户#用户名#\ Documents \ Visual Studio 2010 \ Projects \#项目#\ Test \ Program.cs:第20行

这是否适合您?

 try 
     { 
      DirectoryEntry de = new DirectoryEntry("LDAP://server", "user", "password", AuthenticationTypes.Secure); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex); 
     } 

我假设“服务器”是NetBios名称?尝试使用FQDN。

如果你用自己的证书绑定,您可以使用:

 DirectoryEntry de = new DirectoryEntry("LDAP://server", null, null, AuthenticationTypes.Secure); 

如果你绑定到自己的域名,你可以使用:

DirectoryEntry de = new DirectoryEntry("LDAP://" + Environment.UserDomainName, null, null, AuthenticationTypes.Secure); 

或者干脆:

DirectoryEntry de = new DirectoryEntry(); 

我希望有帮助。

+0

与DirectoryEntry对象的任何绑定失败。我已经使用IP地址和FQDN。仍然是纳达。 但是,如果我在Protocols命名空间中使用LdapConnection类,则可以连接到Ldap。 – mrstebo 2012-03-30 09:09:38

+0

因此,我发布的代码位导致相同的错误?如何:尝试 LdapConnection lc = new LdapConnection(Environment.UserDomainName); lc.Bind(); DirectoryEntry de = new DirectoryEntry(“LDAP://”+ lc.SessionOptions.DomainName); string Name = de.Name; } catch(Exception ex) { Console.WriteLine(ex); } 你可以绑定使用ldp.exe? – Daro 2012-03-30 11:30:54

+0

我一直在使用LdapConnection类,但无论何时需要使用DirectoryEntry实例,它都会在绑定时引发异常(0x80005000)。 – mrstebo 2012-04-02 08:52:41