2017-02-20 88 views
11

我正在嘗試對AD LDS進行ldap查詢以獲取用戶對cn屬性的排序。排序順序規則不應該是默認的英文,但應該按照瑞典語排序。我正在使用.Net中的System.DirectoryServices.Protocols API執行此操作。LDAP按排序規則排序失敗

重現我已安裝偵聽端口389上的AD LDS實例,並安裝了用戶對象類。

使用以下代碼(基地從Performing a Simple Search複製)。訂購規則取自here

public class LdapSorter 
{ 

    public void SearchUsersSorted() 
    { 
     string hostOrDomainName = "localhost"; 
     string targetOu = "cn=Test"; 

     // create a search filter to find all objects 
     string ldapSearchFilter = "(objectClass=user)"; 

     // establish a connection to the directory 
     LdapConnection connection = new LdapConnection(hostOrDomainName); 
     connection.SessionOptions.ProtocolVersion = 3; 

     Console.WriteLine("\r\nPerforming a simple search ..."); 

     try 
     { 
      SearchRequest searchRequest = new SearchRequest 
              (targetOu, 
               ldapSearchFilter, 
               SearchScope.OneLevel, 
               null); 

      searchRequest.Controls.Add(new SortRequestControl("cn", "1.2.840.113556.1.4.1594", false)); 
      //searchRequest.Controls.Add(new SortRequestControl("cn", false)); 
      //searchRequest.Controls.Add(new SortRequestControl("cn", true)); 

      // cast the returned directory response as a SearchResponse object 
      SearchResponse searchResponse = 
         (SearchResponse)connection.SendRequest(searchRequest); 

      Console.WriteLine("\r\nSearch Response Entries:{0}", 
         searchResponse.Entries.Count); 

      // enumerate the entries in the search response 
      foreach (SearchResultEntry entry in searchResponse.Entries) 
      { 
       Console.WriteLine("{0}:{1}", 
        searchResponse.Entries.IndexOf(entry), 
        entry.DistinguishedName); 
      } 
     } 
     catch (DirectoryOperationException e) 
     { 
      Console.WriteLine("\nUnexpected exception occured:\n\t{0}\n{1}", 
           e, e.Response.ErrorMessage); 
      var control = e.Response.Controls.First(c => c is SortResponseControl) as SortResponseControl; 
      if (control != null) 
      { 
       Console.WriteLine("\nControl result: " + control.Result); 
      } 
     } 
    } 
} 

這是輸出:

Performing a simple search ... 

Unexpected exception occured: 
    System.DirectoryServices.Protocols.DirectoryOperationException: The server does not support the control. The control is critical. 
    at System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32 messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut) 
    at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request, TimeSpan requestTimeout) 
    at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryRequest request) 
    at Sort.LdapSorter.SearchUsersSorted() in C:\Source\slask\DotNetSlask\Sort\LdapSorter.cs:line 41 
00000057: LdapErr: DSID-0C090A3D, comment: Error processing control, data 0, v3839 

Control result: InappropriateMatching 

如果使用的不是註釋掉兩個排序請求控件之一,那麼它的工作原理,但與英語的排序順序。

+0

您是否必須將ProtocolVersion設置爲3以支持版本3排序? – david

+0

您爲搜索生成的LDAP命令是什麼,您可以舉個例子來參考嗎?從異常情況或跟蹤情況看,您的問題看起來像是在到達LDAP服務器之前。 –

回答

0

所以,我有兩個主要的猜測,它可能是什麼。首先,(看起來你已經有一些)看看這篇文章。

How to resolve "The server does not support the control. The control is critical." Active Directory error

可能想嘗試在auth部分,看看它是否適合你改變任何東西。其次,您用於排序的OID是瑞典語(可能是故意的),但可能服務器無法使用瑞典語(不包括瑞典語語言包)進行排序(或者是出於某種原因)。您可以嘗試「英語(美國)」選項(1.2.840.113556.1.4.1499),看看是否給你一個不同的結果。

編輯:沒關係,我想我錯過了你的文章的最後一句:)我假設你連接到Windows服務器來運行這些LDAP查詢?如果是這樣,我的猜測是服務器沒有安裝瑞典語言包,但我沒有使用LDAP和外語的經驗,所以沒有保證可以解決它。