遍历属性时,ActiveDirectory错误0x8000500c

我得到以下代码段( 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"]因上述exception而失败
  • 为什么entry.InvokeGet("customAttributeName")有效
  • exception的原因
  • 如何兼顾两者

如果想要从不属于自定义属性所在的域的计算机访问自定义属性(登录用户的凭据无关紧要),则需要传递对象的完全限定名称访问否则客户端计算机上的架构缓存未正确刷新,永远不要调用您所做的所有schema.refresh()调用

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

在这里使用Err.exe工具

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

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

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

我有同样的失败。 我通过列出DirectoryEntry的属性阅读并看到了很多关于错误0x8000500c的问题。 我可以通过Process Monitor(Sysinternals)看到我的进程已经读取了一个模式文件。 此架构文件保存在C:\ Users \ xxxx \ AppData \ Local \ Microsoft \ Windows \ SchCache \ xyz.sch下。

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

我刚刚遇到了这个问题,我的是一个Web应用程序。 我有一些代码可以将用户从IIS中的Windows身份validation中拉出来并从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调用中出现此错误。

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

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

该实用程序直接使用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); 

我从网站上的另一个post中获取了第二行。