2017-02-20 96 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和外语的经验,所以没有保证可以解决它。