我使用.NET Standard LDAP client库来查询我的AD,因为CoreCLR不支持DirectoryServices。
我已经与有效用户建立了密码连接,并使用带有电子邮件过滤器的LdapSearchResults对象来查询AD。
此查询应该只返回一个结果,因为我使用我的AD用户电子邮件进行测试,但是当我循环浏览结果时,我收到了4个条目。
第一个条目对应于我的用户,但其他条目似乎是空的,当循环到达时抛出异常。
using (var cn = new LdapConnection())
{
cn.Connect("<<myHostname>>", 389);
cn.Bind("<<myUserName>>", "<<myPassword>>");
//with the mail filter I was expecting to retrieve only one entry, but I got 4 instead
LdapSearchResults searchResults = cn.Search("<<mysearchBase>>", LdapConnection.SCOPE_SUB, "(&(objectClass=user)(mail=<<myEmail>>))", null, false);
//This loop is executed 4 times
//The entry is correct on the first time
//From the second time all entries are empty and an exception is thrown
while (searchResults.hasMore())
{
LdapEntry nextEntry = null;
try
{
nextEntry = searchResults.next();
}
catch (LdapException e)
{
Console.WriteLine("Error: " + e.LdapErrorMessage);
continue;
}
//Here I extract some information from the entry, like group
Console.WriteLine(nextEntry.getAttribute("memberOf"));
}
}