C# 使用Novell LDAP对.NET Core中的AD进行页面LDAP查询
我使用Novell LDAP库从.NET代码应用程序查询Active Directory。大多数查询都成功,但有些查询返回的结果超过1000个,而AD服务器拒绝了这些结果。因此,我试图找到如何使用Novell的库来分页LDAP查询。我提出的解决方案看起来像C# 使用Novell LDAP对.NET Core中的AD进行页面LDAP查询,c#,.net-core,ldap,C#,.net Core,Ldap,我使用Novell LDAP库从.NET代码应用程序查询Active Directory。大多数查询都成功,但有些查询返回的结果超过1000个,而AD服务器拒绝了这些结果。因此,我试图找到如何使用Novell的库来分页LDAP查询。我提出的解决方案看起来像 public IEnumerable<LdapUser> GetUsers() { this.Connect(); try { var cntRead = 0;
public IEnumerable<LdapUser> GetUsers() {
this.Connect();
try {
var cntRead = 0; // Total users read.
int? cntTotal = null; // Users available.
var curPage = 0; // Current page.
var pageSize = this._config.LdapPageSize; // Users per page.
this.Bind();
this._logger.LogInformation("Searching LDAP users.");
do {
var constraints = new LdapSearchConstraints();
// The following has no effect:
//constraints.MaxResults = 10000;
// Commenting out the following succeeds until the 1000th entry.
constraints.setControls(GetListControl(curPage, pageSize));
var results = this._connection.Search(
this._config.LdapSearchBase,
this.LdapSearchScope,
this._config.LdapUsersFilter,
this.LdapUserProperties,
false,
constraints);
while (results.hasMore() && ((cntTotal == null) || (cntRead < cntTotal))) {
++cntRead;
LdapUser user = null;
try {
var result = results.next();
Debug.WriteLine($"Found user {result.DN}.");
user = new LdapUser() {
AccountName = result.getAttribute(this._config.LdapAccountAttribute)?.StringValue,
DisplayName = result.getAttribute(this._config.LdapDisplayNameAttribute)?.StringValue
};
} catch (LdapReferralException) {
continue;
}
yield return user;
}
++curPage;
cntTotal = GetTotalCount(results);
} while ((cntTotal != null) && (cntRead < cntTotal));
} finally {
this._connection.Disconnect();
}
}
设置约束。MaxResults
似乎对AD服务器没有影响。如果未设置LdapVirtualListControl,则检索将成功,直到检索到第1000个条目
如果使用LdapVirtualListControl
,则在第一次调用results时操作失败。next()
,但出现以下异常:
System.Collections.Generic.KeyNotFoundException: The given key '76' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Novell.Directory.Ldap.Utilclass.ResourcesHandler.getResultString(Int32 code, CultureInfo locale)
at Novell.Directory.Ldap.LdapResponse.get_ResultException()
at Novell.Directory.Ldap.LdapResponse.chkResultCode()
at Novell.Directory.Ldap.LdapSearchResults.next()
at的代码表明这只是一个后续错误,真正的问题是调用失败,错误代码为76,我不知道是什么。因此,我认为我的问题遗漏了一些东西。有什么问题吗?我已经修复了它-以防其他人遇到这种情况: 经过一些互联网研究,我发现错误代码76是什么意思,
ldapvirtualisterresponse
包含更多信息。在我的例子中,错误是-因此似乎分页需要排序控件。
为了解决这个问题,我补充道
constraints.setControls(new[] {
new LdapSortControl(new LdapSortKey("cn"), true),
GetListControl(curPage, pageSize)
});
感谢您提供解决方案!这非常有用。
constraints.setControls(new[] {
new LdapSortControl(new LdapSortKey("cn"), true),
GetListControl(curPage, pageSize)
});