God*_*ter 40 .net c# wcf directoryservices active-directory
我正在尝试使用.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();
Run Code Online (Sandbox Code Playgroud)
我得到以下例外:
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()
Run Code Online (Sandbox Code Playgroud)
作为控制台应用中的代码段,这是有效的.但是,当我将其作为WCF服务的一部分运行(在相同的凭据下运行)时,它会抛出上述异常.
有什么建议?
谢谢
小智 99
我一次又一次地拥有同样的东西,似乎没有任何帮助.
从改变路径ldap://,以LDAP://确实的伎俩.
mar*_*c_s 31
这是一个许可问题.
当您运行控制台应用程序时,该应用程序将使用您的凭据运行,例如"您".
WCF服务运行在哪里?在IIS?最有可能的是,它在一个单独的帐户下运行,该帐户无权查询Active Directory.
您可以尝试使WCF模拟工作正常工作,以便传递您自己的凭据,或者您可以在创建DirectoryEntry时指定用户名/密码:
DirectoryEntry directoryEntry =
new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com",
userName, password);
Run Code Online (Sandbox Code Playgroud)
好的,所以它可能不是凭证(在我看到的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];
}
}
Run Code Online (Sandbox Code Playgroud)
我的想法是:为什么不直接告诉DirectorySearcher你对你感兴趣的属性是什么?然后你不需要再做一个额外的步骤来DirectoryEntry从搜索结果中获取全部内容(应该更快),并且由于你告诉目录搜索者找到该属性,它肯定会被加载到搜索结果中 - 所以除非它是null(没有设置值),那么你应该能够轻松地检索它.
渣
Dav*_*tas 19
在Ektron的上下文中,通过在Windows中安装"IIS6元数据库兼容性"功能解决了此问题:
检查IIS6元数据库兼容性的"Windows功能"或"角色服务",如果缺少则添加:

参考:https://portal.ektron.com/KB/1088/
pg0*_*0xC 11
我有同样的错误 - 在我的情况下,它是路径参数中的额外斜杠,这有所不同.
坏:
DirectoryEntry directoryEntry =
new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com/",
userName, password);
Run Code Online (Sandbox Code Playgroud)
好:
DirectoryEntry directoryEntry =
new DirectoryEntry("LDAP://someserver.contoso.com/DC=contoso,DC=com",
userName, password);
Run Code Online (Sandbox Code Playgroud)
在IIS托管的站点上,尝试回收应用程序池。它解决了我的问题。谢谢
| 归档时间: |
|
| 查看次数: |
101716 次 |
| 最近记录: |