2017-02-24 28 views
3

我正在嘗試將聯繫人寫入ADLDS ldap,以便將它們用作Yealink T48G的電話簿。有時,聯繫人的名字包含一些特殊字符,如「ö」,「ß」和「é」。如果這些字符包含在「givenName」或「displayName」字段中,那麼電話和ldap客戶端都不能正確顯示它們,而是顯示其他一些字符(例如「ö」 - >「ö」),但「名稱「和」dn「字段正確顯示這些字符。C#:如何將特殊字符寫入ADLDS?

如果我通過ADSI-Edit或任何其他工具插入聯繫人值,手機會正確顯示名稱,但我的應用程序不再能夠從givenName讀取插入的特殊字符並顯示一些questionmark-boxes,但是dn並且名稱字段被正確讀取。

我已經嘗試過使用utf-8,utf-16,utf-32,iso-8859-1和windows-1252作爲我的應用程序的編碼。

所以問題是我如何使用C#在ADLDS實例中的inetOrgPerson的givenName屬性中存儲這些特殊字符?

正確顯示:

Corret special characters

錯誤地顯示:

Incorrect special characters

我的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.DirectoryServices.Protocols; 
using System.Net; 

namespace LdapContacts 
{  
    public class LdapClient 
    { 
     private LdapConnection connection; 

     public LdapClient(string host, int port, string distinguishedUsername, string password) 
     { 
      connection = new LdapConnection(new LdapDirectoryIdentifier(host, port)); 
      connection.AuthType = AuthType.Basic; 
      connection.Credential = new NetworkCredential(distinguishedUsername, password); 
      connection.Bind(); 
     } 

     public AddResponse SendAddRequest(string distinguishedName, List<DirectoryAttribute> attributes) 
     { 
      AddRequest request = new AddRequest(distinguishedName, attributes.ToArray()); 
      return connection.SendRequest(request) as AddResponse; 
     } 

     public SearchResponse SendSearchRequest(string distinguishedName, string filter) 
     { 
      SearchRequest request = new SearchRequest(); 
      request.DistinguishedName = distinguishedName; 
      request.Filter = filter; 
      request.Scope = SearchScope.Subtree; 
      return connection.SendRequest(request) as SearchResponse; 
     } 
    } 

    public class ContactsToLdap 
    { 
     private static void Main(string[] args) 
     { 
      LdapClient client = new LdapClient(Settings.LdapHost, Settings.LdapPort, Settings.LdapUsername, Settings.LdapPassword); 

      client.SendAddRequest("CN=Testöäüß,CN=Users,CN=testpart,DC=csdomain,DC=local", new List<DirectoryAttribute>() 
      { 
       new DirectoryAttribute("telephoneNumber", ""), 
       new DirectoryAttribute("objectClass", "inetOrgPerson"), 
       new DirectoryAttribute("uid", "io3e"), 
       new DirectoryAttribute("givenName", "â é testnameöüÄß") 
      }); 
      //distinguished name of contactsfolder 
      SearchResponse result = client.SendSearchRequest(Settings.LdapContactsFolder, "(objectClass=inetOrgPerson)"); 
      foreach (SearchResultEntry sResult in result.Entries) 
      { 
       //display the index of the current entry 
       Console.Write((result.Entries.IndexOf(sResult) + 1) + ":\n"); 
       foreach (DirectoryAttribute attribute in sResult.Attributes.Values) 
       { 
        //output the name of the attribute 
        Console.Write("\t" + attribute.Name + " = "); 
        for (int i = 0; i < attribute.Count; i++) 
        { 
         // convert the attribute to a string if it is an byte[] 
         // output if inserted with ADSI-Edit: ? ? testname???? 
         // output if inserted with this code: â é testnameöüÄß 
         if (attribute[i].GetType().Equals(typeof(byte[]))) 
         { 
          Console.Write(Encoding.UTF8.GetString((byte[])attribute[i]) + "; "); 
         } 
         else 
         { 
          Console.Write(attribute[i] + "; "); 
         } 
        } 
        Console.WriteLine(); 
       } 
       Console.WriteLine(); 
      } 
     } 
    } 
} 

回答

1

這個問題是通過設置應該使用的protocolversion解決Ť o版本3.

connection = new LdapConnection(new LdapDirectoryIdentifier(host, port)); 
connection.SessionOptions.ProtocolVersion = 3; 
+0

Yealink T48G現在顯示的特殊字符是否正確? – boboes

+0

如果您將手機的web界面中的協議版本設置爲版本3,那麼是的。 –