System.DirectoryServices.Protocols.SearchRequest Ldap 查询执行问题

发布于 2025-01-03 21:45:39 字数 1825 浏览 2 评论 0原文

我正在使用 DirectorySearcher 类来查询活动目录。它在一个页面中提供了所有记录(超过 5000 条)。我想每页获取 100 条记录。所以我转向 SearchRequest 类。使用 SearchRequest 类,我每页可以获得 100 条记录。但对于特定查询它不起作用。我想让所有用户的“samaccountname 或显示名称以 'a' 开头”工作正常。然后我想让所有用户的“samaccountname 和显示名称以 'a' 开头”,这是行不通的。我可以猜到原因,一些用户以没有任何显示名称的方式启动他们的 samaccountname。这个问题有什么解决方法吗?请指导我

请参考以下代码

        //This query works fine
        //string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(|(samaccountname=a*)(displayname=a*)))";


        /* Not works */
        string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(&(samaccountname=a*)(displayname=a*)))";
        LdapConnection connection = new LdapConnection(serverName);
        string[] attribs = { "samaccountname", "displayname" };

        // create a SearchRequest object
        SearchRequest searchRequest = new SearchRequest
                                                (scope,
                                                 filter,
                                                 System.DirectoryServices.Protocols.SearchScope.Subtree,
                                                 attribs);

        SortRequestControl sortRequest = new SortRequestControl("samaccountname", false);
        searchRequest.Controls.Add(sortRequest);

        VlvRequestControl vlvRequest =
            new VlvRequestControl(0, numEntries, offsetVal);
        searchRequest.Controls.Add(vlvRequest);

        SearchResponse searchResponse =
            (SearchResponse)connection.SendRequest(searchRequest);

        if (searchResponse.Controls.Length != 2 ||
            !(searchResponse.Controls[0] is SortResponseControl))
        {
            Console.WriteLine("The server does not support VLV");
            return null;
        }

I am using DirectorySearcher class to query the active directory. It gives all the records in a single page (more than 5000). I want to get 100 records per page. So I moved to SearchRequest class. Using SearchRequest class I can get 100 records per page. But for particular query it is not working. I want to get all the users with their "samaccountname or displayname starts with 'a'" works fine. Then I want to get all the users with their "samaccountname and displayname starts with 'a'", this it is not working. I can guess the reason, some of the users starts their samaccountname with a not having any displayname. Any workaround for this issue? Please guide me

Please refer the following code

        //This query works fine
        //string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(|(samaccountname=a*)(displayname=a*)))";


        /* Not works */
        string filter = "(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(&(samaccountname=a*)(displayname=a*)))";
        LdapConnection connection = new LdapConnection(serverName);
        string[] attribs = { "samaccountname", "displayname" };

        // create a SearchRequest object
        SearchRequest searchRequest = new SearchRequest
                                                (scope,
                                                 filter,
                                                 System.DirectoryServices.Protocols.SearchScope.Subtree,
                                                 attribs);

        SortRequestControl sortRequest = new SortRequestControl("samaccountname", false);
        searchRequest.Controls.Add(sortRequest);

        VlvRequestControl vlvRequest =
            new VlvRequestControl(0, numEntries, offsetVal);
        searchRequest.Controls.Add(vlvRequest);

        SearchResponse searchResponse =
            (SearchResponse)connection.SendRequest(searchRequest);

        if (searchResponse.Controls.Length != 2 ||
            !(searchResponse.Controls[0] is SortResponseControl))
        {
            Console.WriteLine("The server does not support VLV");
            return null;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

絕版丫頭 2025-01-10 21:45:39

您确实希望在 Active Directory 中显示名称吗?也许 fullName 或 CN 是更准确的选择。

Do you really want displayName in Active Directory? Perhaps fullName or CN would be a more accurate choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文