【问题标题】:Why has my DirectoryEntry stopped working suddenly?为什么我的 DirectoryEntry 突然停止工作?
【发布时间】:2012-03-29 12:37:28
【问题描述】:

我一直在使用 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);
            }
        }
    }



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

【问题讨论】:

    标签: binding active-directory directoryservices directoryentry


    【解决方案1】:

    这对你有用吗?

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

    希望对你有帮助。

    【讨论】:

    • 与 DirectoryEntry 对象的任何绑定都会失败。我使用了 IP 地址和 FQDN。还是纳达。但是,如果我使用 Protocols 命名空间中的 LdapConnection 类,我可以连接到 Ldap。
    • 所以我发布的那段代码会导致同样的错误?怎么样:试试 { LdapConnection lc = new LdapConnection(Environment.UserDomainName); lc.绑定(); DirectoryEntry de = new DirectoryEntry("LDAP://" + lc.SessionOptions.DomainName);字符串名称 = de.Name; } 捕捉(异常前) { Console.WriteLine(ex);可以用ldp.exe绑定吗?
    • 我一直在使用 LdapConnection 类,但是每当它需要使用 DirectoryEntry 实例时,它就会在绑定时抛出异常(0x80005000)。
    • 您希望检索的对象的 DN 是多少? 20号线是哪个?你提到的变化是什么?
    • 第 20 行是我尝试获取 de.Name 的时间。即使我尝试使用 LDAP://RootDSE(当我在 .NET 代码内部查看时,它似乎是 DirectoryEntry 对象的起点),它也会失败。使用 .NET 3.5 和 4.0 失败。奇怪的是,即使是单独的 AD 浏览器应用程序也无法连接。 AD Explorer 说我输入的服务器名称不是有效的路径名,但可以在我的笔记本电脑上使用!
    猜你喜欢
    • 2019-11-23
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2019-10-15
    • 1970-01-01
    • 2020-03-22
    相关资源
    最近更新 更多