开发者

LDAP DirectorySearcher.sort missing records with not set values

开发者 https://www.devze.com 2023-02-25 01:58 出处:网络
When querying against our AD, we get a full result set on the initial query (samaccounttype=805306368).If we then try to sort the query on a field where the value can be <not set> such as givenn

When querying against our AD, we get a full result set on the initial query (samaccounttype=805306368). If we then try to sort the query on a field where the value can be <not set> such as givenname, it will only return the records with a value set and skips records without the value. We have tried using the below presence flag to include both sets but still loose the not set values when sorting:

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

Not sure if it is related to how the VirtualListView is handling the results set or if there is another problem. Has anyone run in to this before and any suggestions on what I'm missing? Here's the code snip for the ds.virtuallistview sorting.

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

                fo开发者_JS百科reach (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;
            }


Here is an example code of DirectorySearcher which sort the result on 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]);          
     }
   }
 }

You can see in the following result that the givenName is no set for user1 users but the object appears in the result at the end of the list. I do not use the 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

If I add the following line :

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

The result is :

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

The results whithout given Name is missing.

0

精彩评论

暂无评论...
验证码 换一张
取 消