遍历属性时,ActiveDirectory错误0x8000500c

时间:2012-03-16 12:52:08

标签: c# active-directory directoryservices

我收到了以下代码段(SomeName / SomeDomain在我的代码中包含实际值)

var entry = new DirectoryEntry("LDAP://CN=SomeName,OU=All Groups,dc=SomeDomain,dc=com");
foreach (object property in entry.Properties)
{
    Console.WriteLine(property);
}

它为前21个属性打印OK,但随后失败:

COMException {"Unknown error (0x8000500c)"}
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.PropertyEnumerator.get_Entry()
   at System.DirectoryServices.PropertyCollection.PropertyEnumerator.get_Current()
   at ActiveDirectory.Tests.IntegrationTests.ObjectFactoryTests.TestMethod1() in MyTests.cs:line 22

为什么呢?我该如何预防?

更新

这是一个失败的自定义属性。

我在枚举属性之前尝试使用entry.RefreshCache()entry.RefreshCache(new[]{"theAttributeName"})(这没有帮助)。

UPDATE2

entry.InvokeGet("theAttributeName")有效(并且没有RefreshCache)。

有人可以解释原因吗?

UPDATE3

如果我向项目提供FQDN,那么它可以工作:LDAP://srv00014.ssab.com/CN=SomeName,xxxx

恩惠

我正在寻找解决以下问题的答案:

  • 为什么entry.Properties["customAttributeName"]因上述异常而失败
  • 为什么entry.InvokeGet("customAttributeName")有效
  • 异常原因
  • 如何兼顾

5 个答案:

答案 0 :(得分:3)

使用此处的Err.exe工具

http://www.microsoft.com/download/en/details.aspx?id=985

它吐出:
 对于十六进制0x8000500c /十进制-2147463156:
  E_ADS_CANT_CONVERT_DATATYPE adserr.h
 目录数据类型无法转换为/来自本机
 DS数据类型
 找到1个匹配“0x8000500c”

Google搜索“目录数据类型无法转换为/来自本机”并找到此KB: http://support.microsoft.com/kb/907462

答案 1 :(得分:3)

  

如果想要从不是的机器访问自定义属性   自定义属性所在的域的一部分(凭据   登录用户无关紧要)一个人需要完全通过   对象的限定名称正在尝试访问模式   客户端计算机上的缓存未正确刷新,请不要全部清除   schema.refresh()调用你做

找到here。考虑到对问题的更新,这听起来像是你的问题。

答案 2 :(得分:1)

我有同样的失败。我通过列出DirectoryEntry中的属性阅读并看到了很多关于错误0x8000500c的问题。 我可以通过Process Monitor(Sysinternals)看到我的进程已经读取了一个模式文件。此架构文件保存在 C:\用户\ XXXX \应用程序数据\本地\微软\的Windows \ SchCache \ xyz.sch

删除此文件,该程序正常工作:)

答案 3 :(得分:0)

我刚刚遇到了这个问题,我的是一个Web应用程序。 我有一些代码可以将用户从IIS中的Windows身份验证中删除,并从AD中提取信息。

using (var context = new PrincipalContext(ContextType.Domain))
{
    var name = UserPrincipal.Current.DisplayName;
    var principal = UserPrincipal.FindByIdentity(context, this.user.Identity.Name);
    if (principal != null)
    {
        this.fullName = principal.GivenName + " " + principal.Surname;
    }
    else
    {
        this.fullName = string.Empty;
    }
}

这在我的测试中运行良好,但是当我发布网站时,会在FindByIdentity调用中出现此错误。

我通过使用正确的用户来修复网站的应用池。一旦我解决了这个问题,就开始工作了。

答案 4 :(得分:0)

我对奇怪数据类型的自定义属性有同样的问题。我有一个实用程序可以提取值,但是服务中的一些结构化代码却没有。

该实用程序直接使用SearchResult对象,而该服务使用的是DirectoryEntry。

它被提炼出来了。

SearchResult result;

result.Properties[customProp];     // might work for you
result.Properties[customProp][0];  // works for me. see below

using (DirectoryEntry entry = result.GetDirectoryEntry())
{
    entry.Properties[customProp]; // fails
    entry.InvokeGet(customProp);  // fails as well for the weird data
}

我的直觉是,SearchResult不再是一个执行者,而是返回它所拥有的一切。

当它转换为DirectoryEntry时,此代码会破坏奇怪的数据类型,甚至InvokeGet也会失败。

带有extra [0]的实际提取代码如下所示:

byte[] bytes = (byte[])((result.Properties[customProp][0]));
String customValue = System.Text.Encoding.UTF8.GetString(bytes);

我从网站上的另一个帖子中选择了第二行。

相关问题