我正在尝试使用.Net中的目录服务运行一个简单的LDAP查询。
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com");
directoryEntry.AuthenticationType = AuthenticationTypes.Secure;
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", username);
var result = directorySearcher.FindOne();
var resultDirectoryEntry = result.GetDirectoryEntry();
return resultDirectoryEntry.Properties["msRTCSIP-PrimaryUserAddress"].Value.ToString();我得到了以下异常:
System.Runtime.InteropServices.COMException (0x80005000): Unknown error (0x80005000)
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
at System.DirectoryServices.DirectoryEntry.Bind()
at System.DirectoryServices.DirectoryEntry.get_AdsObject()
at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
at System.DirectoryServices.DirectorySearcher.FindOne()作为控制台应用程序中的一个代码片段,这是可行的。但当我将其作为WCF服务的一部分运行时(使用相同的凭据运行),它会抛出上述异常。
有什么建议吗?
谢谢
发布于 2009-11-12 22:07:32
这是一个权限问题。
当您运行控制台应用程序时,该应用程序将使用您的凭据运行,例如“您”。
WCF服务在哪里运行?在IIS中?最有可能的情况是,它在一个单独的帐户下运行,该帐户不允许查询Active Directory。
您可以尝试让WCF模拟工具工作,以便传递您自己的凭据,或者您可以在创建DirectoryEntry时指定用户名/密码:
DirectoryEntry directoryEntry =
new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com",
userName, password);好吧,所以说到底可能不是凭据的问题(我看到的80%以上的情况通常都是这样)。
稍微修改一下你的代码怎么样?
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
directorySearcher.Filter = string.Format("(&(objectClass=user)(objectCategory=user) (sAMAccountName={0}))", username);
directorySearcher.PropertiesToLoad.Add("msRTCSIP-PrimaryUserAddress");
var result = directorySearcher.FindOne();
if(result != null)
{
if(result.Properties["msRTCSIP-PrimaryUserAddress"] != null)
{
var resultValue = result.Properties["msRTCSIP-PrimaryUserAddress"][0];
}
}我的想法是:为什么不直接告诉DirectorySearcher你对什么属性感兴趣呢?然后你不需要做额外的步骤来从搜索结果中获得完整的DirectoryEntry (应该更快),既然你告诉目录搜索器找到那个属性,它肯定会被加载到搜索结果中-所以除非它是null (没有设置值),否则你应该能够很容易地检索到它。
Marc
发布于 2012-04-11 14:55:45
我做了一次又一次同样的事情,但似乎没有任何帮助。
将路径从ldap://更改为LDAP://确实起到了作用。
发布于 2014-07-01 09:02:29
在Ektron的环境下,这个问题可以通过在IIS6中安装“Windows元数据库兼容性”功能来解决:
对于Windows元数据库兼容性,请检查“
功能”或“角色服务”,如果缺少,请添加:

https://stackoverflow.com/questions/1722398
复制相似问题