如何在具有大量用户的 Ldap 服务器上进行分页搜索?

Ade*_*ina 5 c# ldap active-directory .net-core

我需要使用 .NET Core 创建一个在 Linux 上运行的 LDAP 客户端。我在互联网上搜索,唯一支持 .Net 标准的库是 Novell.Directory.Ldap (开源,iei - https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard)。目录服务库在 Linux 的 .Net Core 中不支持,仅在 Windows 上支持。

我查看了文档并成功创建了一个基本的 Ldap 客户端应用程序。

现在的问题是:我需要同步很多用户(10.000、200.000 个用户),默认情况下我的 ldap 服务器的最大大小页面为 1000(我不想更改它)。我使用 VirtualListControl 来创建页面,它对于 10k 用户来说工作正常。

对于 20 万用户,它会崩溃并显示错误 53 - 不愿意执行LdapSortControl 响应。Novell 库需要 LdapSortControl 才能执行分页操作(用于索引),我认为我的 ldap 无法排序 200k。我使用的代码:

        int startIndex = 1;
        int contentCount = 0;
        int afterIndex = 10;
        int count = 0;
        do
        {
            LdapVirtualListControl ctrl = new LdapVirtualListControl(startIndex, 0, afterIndex, contentCount);
            LdapSortKey[] keys = new LdapSortKey[1];
            keys[0] = new LdapSortKey("sn");
            LdapSortControl sort = new LdapSortControl(keys, true);

            LdapSearchConstraints constraints = _ldapConnection.SearchConstraints;
            constraints.setControls(new LdapControl[] { ctrl, sort});

            _ldapConnection.Constraints = constraints;
            LdapSearchResults lsc = _ldapConnection.Search(searchBase, searchScope, searchFilter, attributes, typesOnly, cons);
            while (lsc.HasMore())
            {
                try
                {
                    LdapEntry nextEntry = lsc.Next();
                    Console.WriteLine( nextEntry.DN);
                }
                catch (LdapException e)
                {
                    Console.WriteLine($"Error: {e.LdapErrorMessage}");
                    //Exception is thrown, go for next entry
                    continue;
                }

            }

            LdapControl[] controls = lsc.ResponseControls;
            if (controls == null)
            {
                Console.Out.WriteLine("No controls returned");
            }
            else
            {
                foreach (LdapControl control in controls)
                {
                    if (control.ID == "2.16.840.1.113730.3.4.10")
                    {
                        LdapVirtualListResponse response = new LdapVirtualListResponse(control.ID, control.Critical, control.getValue());
                        startIndex += afterIndex + 1;
                        contentCount = response.ContentCount;
                        count += afterIndex;
                    }
                }
            }
            Console.WriteLine(i);

        } while (count <= contentCount);
Run Code Online (Sandbox Code Playgroud)

该文档很小,没有足够的信息,我不知道如何更好地使用 Novell 库进行分页。这里有人使用 Novell Ldap 库并且有分页经验并且可以帮助我吗?我已四散

谢谢

IGi*_*nny 2

要使用 Novell.Directory.Ldap 进行分页查询,您必须使用 LdapVirtualListControl 作为“请求”控件。

LdapVirtualListControl 遵循 Ldap 排序请求控制:VLV(虚拟列表视图)的参数,即:

before:after:index:content_count
Run Code Online (Sandbox Code Playgroud)

其中“ before ”是您希望在索引之前返回的项目数,“ after ”是您希望在索引之后返回的项目数,“ content_count ”是服务器中预期的项目总数。如果您不知道,则必须使用 0 作为值。

如果您想通过“ldapsearch”cli 返回前 5 个元素,则必须使用:'0:4:1:0' 和 '0:4:5:0' 来返回后续的 5 个元素。

LdapVirtualListControl 拥有一个具有相同参数但顺序不同的构造函数:

LdapVirtualListControl(int startIndex, int beforeCount, int afterCount, int contentCount)
Run Code Online (Sandbox Code Playgroud)

就我个人而言,我使用此函数来正确设置参数:

public static LdapSearchConstraints AddPagination(this LdapSearchConstraints constraints, int page,
            int pageSize)
{
    int startIndex = (page - 1) * pageSize;
    startIndex++;
    int beforeCount = 0;
    int afterCount = pageSize - 1;
    int contentCount = 0; //0 means that i don't know the total count

    var lvlc = new LdapVirtualListControl(startIndex, beforeCount, afterCount, contentCount);
    constraints.setControls(lvlc);
    return constraints;
}
Run Code Online (Sandbox Code Playgroud)

之后,另一个问题需要您注意:如果您请求一组数据,这些数据位于数据集末尾之后,您将收到数据集的第一项。

解释:

LDAP 中存在的数据示例:
| 1 | 2 | 3 | 4 |
如果我们要一套
________| 3 | 4 | 5 | <- 5 不存在
Ldap 返回:
________| 3 | 4 | 1 | <-一切从头开始

为了解决这个问题,我在返回之前删除了超出的元素:

var lastIndex = (page * pageSize);
if (lastIndex > result.Total)
{
    var itemsToReturn = (int) (result.Total - (lastIndex - pageSize));
    if (itemsToReturn < 1)
    {
        items = new List<LdapQueryItem>();
    }
    else
    {
        items = items.Take(itemsToReturn).ToList();
    }
}   
Run Code Online (Sandbox Code Playgroud)

最后,获取总计的函数(在 searchResults.HasMore() 方法之后执行)

protected int? GetTotal(LdapSearchResults searchResult)
{
    if (searchResult == null) {
       throw new ArgumentNullException(nameof(searchResult));
    }
    if (searchResult.ResponseControls != null && searchResult.ResponseControls.Any())
    {
        foreach (LdapControl control in searchResult.ResponseControls)
        {
            if (control.ID == "2.16.840.1.113730.3.4.10") // the id of the response control
            {
                LdapVirtualListResponse response =
                    new LdapVirtualListResponse(control.ID, control.Critical, control.getValue());
                return response.ContentCount;
            }
        }
    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

您可以在本书中获得更多见解和信息: Understanding and Deploying LDAP Directory Services