C# 使用Novell.Directory.Ldap.NETStandard从AD读取所有用户

C# 使用Novell.Directory.Ldap.NETStandard从AD读取所有用户,c#,ldap,C#,Ldap,我需要阅读广告中的所有用户。以下是我正在使用的代码: using Novell.Directory.Ldap; using Novell.Directory.Ldap.Controls; using System.Linq; namespace LdapTestApp { class Program { static void Main() { LdapConnection ldapConn = new LdapConne

我需要阅读广告中的所有用户。以下是我正在使用的代码:

using Novell.Directory.Ldap;
using Novell.Directory.Ldap.Controls;
using System.Linq;

namespace LdapTestApp
{
    class Program
    {
        static void Main()
        {
            LdapConnection ldapConn = new LdapConnection();
            ldapConn.SecureSocketLayer = true;
            ldapConn.Connect(HOST, PORT);

            try
            {
                var cntRead = 0;
                int? cntTotal = null;
                var curPage = 0;

                ldapConn.Bind(USERNAME, PASSWORD);

                do
                {
                    var constraints = new LdapSearchConstraints();
                    constraints.SetControls(new LdapControl[]
                    {
                        new LdapSortControl(new LdapSortKey("sn"), true),
                        new LdapVirtualListControl("sn=*", 0, 10)
                    });

                    ILdapSearchResults searchResults = ldapConn.Search(
                        "OU=All Users,DC=homecredit,DC=ru",
                        LdapConnection.ScopeSub,
                        "(&(objectCategory=person)(objectClass=user))",
                        null,
                        false,
                        constraints
                    );

                    while (searchResults.HasMore() && ((cntTotal == null) || (cntRead < cntTotal)))
                    {
                        ++cntRead;

                        try
                        {
                            LdapEntry entry = searchResults.Next();
                        }
                        catch (LdapReferralException)
                        {
                            continue;
                        }
                    }

                    ++curPage;
                    cntTotal = GetTotalCount(searchResults as LdapSearchResults);
                } while ((cntTotal != null) && (cntRead < cntTotal));
            }
            finally
            {
                ldapConn.Disconnect();
            }
        }

        private static int? GetTotalCount(LdapSearchResults results)
        {
            if (results.ResponseControls != null)
            {
                var r = (from c in results.ResponseControls
                         let d = c as LdapVirtualListResponse
                         where (d != null)
                         select (LdapVirtualListResponse)c).SingleOrDefault();
                if (r != null)
                {
                    return r.ContentCount;
                }
            }

            return null;
        }
    }
}

我做错了什么?

VLV是浏览索引,与浏览大量条目的可能性或不浏览大量条目没有直接关系(请参阅)。因此,即使此控件在您的广告上被激活,您也无法通过这种方式检索超过1000个元素:

  • 广告默认情况下,
    MaxPageSize
    为1000(请参阅)
因此,您可以做什么:

  • 使用特定的分页结果控件,但Novell C#LDAP库
  • 问您这样一个问题:“这与查找单个请求中的所有用户有关吗?”(您的请求看起来像批处理请求:请记住,LDAP服务器的设计目的与经典数据库不同,后者可以轻松返回数百万个条目,这就是为什么大多数LDAP目录的默认大小限制在1000左右的原因)
答案是否:检查您的设计,在LDAP搜索过滤器、搜索库等方面更加具体

答案是肯定的:

  • 您只有一个广告服务器:请管理员更改
    MaxPageSize
    值,但此设置是全局设置,可能会导致一些副作用(例如,如果每个人都开始一直请求所有用户,会发生什么情况?)
  • 您有多个广告服务器:您可以为特定的“类批处理”查询配置一个,就像您尝试执行的查询一样(如此大的
    MaxPageSize
    ,大量超时等)

我不得不使用这里描述的方法:


解决方案远非完美,但至少我能够继续前进。

从3.5版开始,该库支持简单的分页结果控制,使用方法与ldapConnection一样简单。使用SimplePaging(searchOptions,pageSize)或ldapConnection进行搜索。使用SimplePaging进行搜索(ldapEntryConverter,searchOptions,pageSize)-有关更多详细信息,请参阅Github repo,更具体地说,可以将测试用作使用示例。

它可能来自服务器端禁用的控件。你能试试你的代码去激活你的
LdapSortControl
,然后
LdapVirtualListControl
,看看会发生什么吗?@MaxXapi不带行[new LdapVirtualListControl(“sn=*”,0,10)]它可以工作。这是否意味着我不能使用LdapVirtualListControl?我怎样才能从广告中获得1000多名用户?
"Unavailable Critical Extension"
000020EF: SvcErr: DSID-03140594, problem 5010 (UNAVAIL_EXTENSION), data 0