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

时间:2012-02-09 07:52:37

标签: active-directory ldap

我正在使用DirectorySearcher类来查询活动目录。它在一个页面中提供所有记录(超过5000)。我想每页获得100条记录。所以我转到了SearchRequest类。使用SearchRequest类我每页可以获得100条记录。但对于特定的查询它不起作用。我想让所有用户的“samaccountname或displayname以'a'开头”工作正常。然后我想让所有用户的“samaccountname和displayname以'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;
        }

1 个答案:

答案 0 :(得分:0)

你真的想在Active Directory中使用displayName吗?也许fullName或CN将是更准确的选择。