gpt4 book ai didi

c# - 如何使用 Novell.Directory.Ldap.NETStandard 和 Simple Paged Results 控件在 Ldap 服务器上进行分页搜索?

转载 作者:行者123 更新时间:2023-12-03 23:10:29 25 4
gpt4 key购买 nike

我正在尝试使用 Novell.Directory.Ldap.NETStandard ( https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard ) 和简单分页结果控件 ( https://ldapwiki.com/wiki/Simple%20Paged%20Results%20Control ) 在 Active Directory 中进行分页搜索。

第一页工作正常,但第二页在 searchResult.next() 行上抛出“不可用的关键扩展”。在查看 ActiveDirectory 的事件日志时,我发现:

00000057:LdapErr:DSID-0C090809,注释:错误处理控制,数据0,v23f0
0000208D:NameErr:DSID-03100213,问题 2001(NO_OBJECT),数据 0,最佳匹配:

我们也尝试过 LdapVirtualListControl 但遇到了不同的问题,请参阅 How to do a paged search on an Ldap server with > 10000 entries using Novell.Directory.Ldap.NETStandard?

这是我们用来重现的简化代码:

        // Connection
var ldapConn = new LdapConnection()
{
SecureSocketLayer = true,
};
ldapConn.UserDefinedServerCertValidationDelegate += (sender, certificate, chain, sslPolicyErrors) => true;
ldapConn.Connect(host, 636);
ldapConn.Bind(username, password);

// Constraints
LdapSearchConstraints searchConstraints = (LdapSearchConstraints)_conn.SearchConstraints.Clone();
int pageSize = 100, count = 0;
bool exit = false;
const string LDAP_SERVER_SIMPLE_PAGED_RESULT_OID = "1.2.840.113556.1.4.319";

LdapControl pageControl = null;

do
{
int inPageCount = 0;

// Add Simple Paged Result control
var request = new Asn1Sequence(2);
request.add(new Asn1Integer(pageSize));
request.add(pageControl == null ? new Asn1OctetString("") : new Asn1OctetString(pageControl.getValue()));
searchConstraints.setControls(
new LdapControl(LDAP_SERVER_SIMPLE_PAGED_RESULT_OID, true, request.getEncoding(new LBEREncoder()))
);

// Get search result
var searchResult = (LdapSearchResults)ldapConn.Search(container, LdapConnection.SCOPE_SUB, query, null, false, searchConstraints);
while (searchResult.hasMore())
{

// Detect simple paged result control
pageControl = searchResult.ResponseControls?.Where(rc => rc.ID == LDAP_SERVER_SIMPLE_PAGED_RESULT_OID).FirstOrDefault();
if (pageControl != null) break;

var nextEntry = searchResult.next();
inPageCount++;

}
count += inPageCount;

// Exit if no more pages
exit = pageControl == null;

} while (!exit);

最佳答案

为什么代码不起作用

根据 RFC简单分页结果控件编码为
realSearchControlValue ::= SEQUENCE {
size INTEGER (0..maxInt),
-- requested page size from client
-- result set size estimate from server
cookie OCTET STRING
}
在下一个屏幕截图(取自 Wireshark)中可以清楚地看到这一点。

:

当客户端向请求添加控制时,大小被设置为页面中所需的元素数量,cookie 是来自先前服务器响应的不透明结构(第一个请求为空)。

当您尝试在请求中构造控件时,您错误地添加了整个控件值而不是 cookie (pageControl.getValue()):

 var request = new Asn1Sequence(2);
request.add(new Asn1Integer(pageSize));
request.add(pageControl == null ? new Asn1OctetString("") : new Asn1OctetString(pageControl.getValue()));

它使第一个请求之后的所有请求都不正确。

建议的解决方案

看看 https://github.com/metacube/PagedResultsControl .
我创建了类型化的简单分页结果控制实现,它封装了解码/编码逻辑。在来自 Active Directory 的 100 000 多个条目的情况下,对我来说效果很好。

测试应用程序显示了基本用法。

关于c# - 如何使用 Novell.Directory.Ldap.NETStandard 和 Simple Paged Results 控件在 Ldap 服务器上进行分页搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58472427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com