首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#:如何在ADLDS中写入特殊字符?

C#:如何在ADLDS中写入特殊字符?
EN

Stack Overflow用户
提问于 2017-02-24 23:43:32
回答 1查看 513关注 0票数 3

我正在尝试将联系人写入ADLDS ldap,以便将它们用作Yealink T48G的电话簿。有时,联系人的名称包含一些特殊字符,如"ö“、”?“和"é”。如果这些字符包含在"givenName“或"displayName”字段中,则电话和ldap客户端都不能正确显示它们,而是显示一些其他字符(例如"ö“->”?“),但是"name”和"dn“字段将正确显示这些字符。

如果我通过ADSI编辑或任何其他工具插入联系人值,电话将正确显示名称,但我的应用程序不再能够从givenName读取插入的特殊字符,并显示一些问号框,但dn和名称字段被正确读取。

我已经尝试使用utf-8、utf-16、utf-32、iso-8859-1和windows-1252作为我的应用程序的编码。

因此,问题是如何在ADLDS实例中的inetOrgPerson的givenName属性中使用C#来存储这些特殊字符?

正确显示:

显示错误:

我的代码:

代码语言:javascript
复制
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", "0123456"),
                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();
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-08 17:47:41

已通过将应使用的协议版本设置为版本3解决此问题。

代码语言:javascript
复制
connection = new LdapConnection(new LdapDirectoryIdentifier(host, port));
connection.SessionOptions.ProtocolVersion = 3;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42442696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档