LDAP DirectorySearcher.sort缺少未设置值的记录

时间:2011-04-16 14:36:21

标签: model-view-controller active-directory ldap

在查询AD时,我们会在初始查询(samaccounttype=805306368)上获得完整的结果集。然后,如果我们尝试在值<not set>的字段(例如givenname)上对查询进行排序,它将仅返回具有值集的记录并跳过没有值的记录。我们尝试使用以下在线状态标志来包含两个集合但在排序时仍然松散未设置值:

(|(&(samaccounttype=805306368)(givenName=*))(&(samaccounttype=805306368)(!givenname=*)))

不确定它是否与VirtualListView处理结果集或是否存在其他问题有关。有没有人在此之前遇到过这个问题以及我缺少什么的建议?这是ds.virtuallistview排序的代码片段。

using (var ds = new DirectorySearcher(de))
            {
                ds.Filter = Filter;

                foreach (var p in Properties)
                {
                    ds.PropertiesToLoad.Add(p.LDAPName);
                }

                //get record count
                ds.PropertyNamesOnly = false;
                ds.Sort = new SortOption(this.Properties.PrimaryOrderBy.LDAPName, SortDirection.Ascending); //ldap must always return ascending so we can custom sort
                ds.VirtualListView = new DirectoryVirtualListView(0, 0, 0);
                foreach(SearchResult s in ds.FindAll()){ /*must enumerate the collection before calling approximate total*/ }
                Records = ds.VirtualListView.ApproximateTotal;
                results = ds.FindAll();
                //Records = results.Count;
            }

1 个答案:

答案 0 :(得分:0)

以下是DirectorySearcher的示例代码,该代码对givenName上的结果进行排序。

 static void Main(string[] args)
 {
   /* Connection to Active Directory
    */
   DirectoryEntry deBase = new DirectoryEntry("LDAP://WM2008R2ENT:389/dc=dom,dc=fr");

   /* Directory Search
    */
   DirectorySearcher dsLookFor = new DirectorySearcher(deBase);
   dsLookFor.Filter = "(sn=users)";
   dsLookFor.SearchScope = SearchScope.Subtree;
   dsLookFor.PropertiesToLoad.Add("cn");
   dsLookFor.PropertiesToLoad.Add("givenName");
   dsLookFor.PropertiesToLoad.Add("telephoneNumber");

   dsLookFor.Sort = new SortOption("givenName", SortDirection.Descending);

   SearchResultCollection srcUsers = dsLookFor.FindAll();

   foreach (SearchResult sruser in srcUsers)
   {
     Console.WriteLine("{0}", sruser.Path);

     foreach (string property in sruser.Properties.PropertyNames)
     {
       Console.WriteLine("\t{0} : {1} ", property, sruser.Properties[property][0]);          
     }
   }
 }

您可以在以下结果中看到givenName没有为user1 users设置,但该对象显示在列表末尾的结果中。我不使用VirtualListView

LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr
        givenname : user0
        telephonenumber : 88
        adspath : LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr
        cn : user0 users
LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr
        givenname : user2
        adspath : LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr
        cn : user2 users
LDAP://WM2008R2ENT:389/CN=user1 users,OU=MonOu,DC=dom,DC=fr
        telephonenumber : 99
        adspath : LDAP://WM2008R2ENT:389/CN=user1 users,OU=MonOu,DC=dom,DC=fr
        cn : user1 users

如果我添加以下行:

dsLookFor.VirtualListView = new DirectoryVirtualListView(1,0,2);

结果是:

LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr
        givenname : user0
        telephonenumber : 88
        adspath : LDAP://WM2008R2ENT:389/CN=user0 users,OU=MonOu,DC=dom,DC=fr
        cn : user0 users
LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr
        givenname : user2
        telephonenumber : 55
        adspath : LDAP://WM2008R2ENT:389/CN=user2 users,OU=MonOu,DC=dom,DC=fr
        cn : user2 users

没有给出名字的结果缺失。