C#使用DirectoryVirtualListView分页ldap查询结果

C# using DirectoryVirtualListView to page ldap query results

我正在尝试从 LDAP 服务器获取用户。服务器每次响应限制为 200 个结果,我想获得所有超过 5000 的用户。所以我需要进行某种分页。到目前为止,我已经尝试了两种方法:

  1. 正在设置 DirectorySearcher PageSize 属性。这种方法会抛出一个异常,显示“[DSA]:需要链接 (DSP) 时无法分页”。这是代码
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://server.name");
rootEntry.Username = "cn=any,o=any,dc=any,dc=any";
rootEntry.Password = "thepassword";
rootEntry.AuthenticationType = AuthenticationTypes.FastBind; 
using (DirectorySearcher searcher = new DirectorySearcher(rootEntry))
{
    searcher.PageSize = 50;
    searcher.Filter = "(&(objectClass=person))";
    foreach (SearchResult sr in searcher.FindAll())
    {
        Console.WriteLine(sr.Path);
    }
}
  1. 使用 VirtualListView。这样也抛出异常"VirtualListView property value cannot be set",代码:
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://server.name", "cn=any,o=any,dc=any,dc=any", "any", AuthenticationTypes.FastBind);
using (DirectorySearcher searcher = new DirectorySearcher(rootEntry))
{
    searcher.Filter = "(&(objectClass=person))";
    searcher.Sort = new SortOption("cn", SortDirection.Ascending);
    searcher.VirtualListView = new DirectoryVirtualListView(25);

    foreach (SearchResult sr in searcher.FindAll())
    {
        Console.WriteLine(sr.Path);
    }
}

谁能告诉我我做错了什么?。提前致谢。

using (var directoryEntry = new DirectoryEntry(@"LDAP://TestDomain")
{
    Username = @"TestDomain\TestUser",
    Password = "T@st#1"
})
{
    var directorySearcher = new DirectorySearcher(directoryEntry)
    {
        VirtualListView = new DirectoryVirtualListView(0, 9, 1)
    };

    // will give you only users with mail
    var filter = "(&(objectCategory=person)(objectClass=user)(mail=*))";

    directorySearcher.Filter = filter;
    directorySearcher.Sort = new SortOption() { PropertyName = "displayname", 
                                                 Direction = SortDirection.Ascending };

    directorySearcher.PropertiesToLoad.Add("displayname");
    directorySearcher.PropertiesToLoad.Add("mail");
    directorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
    directorySearcher.SizeLimit = 100;

    var results = directorySearcher.FindAll();
    var names = new List<string>();

    foreach (SearchResult r in results)
    {
        // Map the result 
    }
}

您必须尝试一下,默认情况下它会按 cn

对用户进行排序
1. VirtualListView = new DirectoryVirtualListView(0, 9, 1)
2. VirtualListView = new DirectoryVirtualListView(0, 49, 100);
  1. 它会给你从 0 到 9 的 10 条记录
  2. 将从 100 个开始获得 50 个用户。