【问题标题】:Read all users from AD using Novell.Directory.Ldap.NETStandard使用 Novell.Directory.Ldap.NETStandard 从 AD 读取所有用户
【发布时间】:2020-12-29 04:07:52
【问题描述】:

我需要从 AD 读取所有用户。这是我正在使用的代码:

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;
        }
    }
}

我使用这个问题Page LDAP query against AD in .NET Core using Novell LDAP 作为基础。 不幸的是,当我尝试接收第一个条目时出现此异常:

"Unavailable Critical Extension"
000020EF: SvcErr: DSID-03140594, problem 5010 (UNAVAIL_EXTENSION), data 0

我做错了什么?

【问题讨论】:

  • 它可能来自服务器端禁用的控件。你可以试试你的代码停用你的LdapSortControl,然后LdapVirtualListControl,然后看看会发生什么?
  • @MaxXapi 没有行 [new LdapVirtualListControl("sn=*", 0, 10)] 它可以工作。这是否意味着我不能使用 LdapVirtualListControl?那我怎样才能从 AD 中获取所有 1000+ 用户呢?

标签: c# ldap


【解决方案1】:

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

那么你可以做什么:

  • 使用特定的分页结果控件,但似乎Novell C# LDAP 库does not have one
  • 问您一个问题:“这是否适合在单个请求中查找所有用户?” (您的请求看起来像一个批处理请求:请记住,LDAP 服务器的设计目的与经典数据库不同 - 它可以轻松返回数百万个条目 - 这就是为什么大多数 LDAP 目录的默认大小限制在 1000 左右)。

答案是:检查您的设计,在 LDAP 搜索过滤器、搜索库等方面更加具体。

答案是是的

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

【讨论】:

    【解决方案2】:

    我不得不使用这里描述的方法: https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard/issues/71#issuecomment-420917269

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

    【讨论】:

      【解决方案3】:

      从 3.5 版开始,该库支持简单分页结果控件 - https://ldapwiki.com/wiki/Simple%20Paged%20Results%20Control - 用法与 ldapConnection.SearchUsingSimplePaging(searchOptions, pageSize) 或 ldapConnection.SearchUsingSimplePaging(ldapEntryConverter, searchOptions, pageSize) 一样简单 - 请参阅 Github repo更多细节 - https://github.com/dsbenghe/Novell.Directory.Ldap.NETStandard,更具体地说,将测试用作使用示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-31
        • 2020-07-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-02
        相关资源
        最近更新 更多