1

我一直在使用 DirectoryServices 命名空间中的 DirectoryEntry 对象,并且进展顺利。但是我对我的一个 LDAP 类进行了更改,突然间它停止了在我的程序的 32 位版本上工作。

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


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

即使是这个简单的代码也失败了(这并不奇怪,因为绑定是一个关键部分!)

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,但它只是挂起。


Daro 建议的结果:
代码

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



日志文件产生: System.DirectoryServices.DirectoryEntry.Bind() 的 System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)的
未知错误 (0x80005000)的 System.DirectoryServices.DirectoryEntry.get_Name () 的 Test.Program.Main()在 C:\Users\#username#\Documents\Visual Studio 2010\Projects\#project#\Test\Program.cs:line 20




4

1 回答 1

0

这对你有用吗?

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

我希望这会有所帮助。

于 2012-03-30T07:52:59.130 回答