AD抛出COMException,错误代码为-2147463168

时间:2013-11-25 09:34:32

标签: c# active-directory comexception

Unbekannter Fehler (0x80005000) bei System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
bei System.DirectoryServices.DirectoryEntry.Bind()
bei System.DirectoryServices.DirectoryEntry.get_AdsObject()
bei System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)
bei System.DirectoryServices.DirectorySearcher.FindAll() 

这是返回的内容。我的代码如下:

string domain = DNFilterEntry.Key;
DirectoryEntry entry = new DirectoryEntry("LDAP://" + ((domain != "") ? "DC=" + domain : ""));
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.PropertiesToLoad.Add("displayName");
searcher.PropertiesToLoad.Add("mail");
searcher.Filter = string.Format("(|"+DNfilterEntry.Value+")",filterparams.ToArray());
try
{
    SearchResultCollection users = searcher.FindAll(); // error thrown here
    ...
}
catch (System.Runtime.InteropServices.COMException ex) { Debug.Message(ex.ErrorCode + ex.Message + ex.StackTrace); continue; }

键值对DNfilterEntry为"" => "(mail={0})(mail={1})"

和filterparams是包含两个项somemail@intra.domain.comsomeothermail@intra.domain.com的列表。过滤器生成正确

(mail=somemail@intra.domain.com)(mail=someothermail@intra.domain.com)

(使用VS调试器证明;两个邮件地址都有效)

此处的任何人都知道错误发生的原因吗?

编辑:以下不同查询的代码可以正常运行:

string Filter="(&";
switch (filterType) {
    case 0: Filter += "(sAMAccountType=805306368)"; break;
    case 1: Filter += "(objectCategory=group)"; break;
    case 2: Filter += "(|(objectCategory=group)(objectCategory=person))"; break;
}
Filter += "(sAMAccountName={0}))";
DirectorySearcher searcher = new DirectorySearcher();
searcher.Filter = string.Format(Filter, username);
SearchResult user = searcher.FindOne();
if (user == null || user.Properties == null || user.Properties[propName] == null || user.Properties[propName][0] == null) return null;
return user.Properties[propName][0].ToString();

propName =“mail”,username =“someusername”,domain =“”,filterType = 0它返回someusername的主电子邮件地址。

EDIT2:

当我将DirectorySearcher searcher = new DirectorySearcher(entry);更改为DirectorySearcher searcher = new DirectorySearcher();

时,有人可以告诉我它为什么有效

0 个答案:

没有答案