首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Mono LdapException上运行Exchange

在Mono LdapException上运行Exchange
EN

Stack Overflow用户
提问于 2013-03-07 19:16:56
回答 1查看 1.4K关注 0票数 5

我正在尝试通过Mono (版本2.10.8.1 & 3.0.6)在debian上使用Exchange 2,我正在使用vs2012在windows 8上进行开发。

这个程序在windows上运行得很好,我得到了预期的输出。

但是,在mono上,我一直得到以下输出和异常。

代码语言:javascript
复制
<Trace Tag="AutodiscoverConfiguration" Tid="1" Time="2013-03-07 19:09:05Z">
Starting SCP lookup for domainName='example.com', root path=''
</Trace>
Connect Error

Unhandled Exception: LdapException: (91) Connect Error
System.Net.Sockets.SocketException: No such host is known
  at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[]          h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpClient.Connect (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpClient..ctor (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
  at Novell.Directory.Ldap.Connection.connect (System.String host, Int32 port, Int32 semaphoreId) [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: LdapException: (91) Connect Error
System.Net.Sockets.SocketException: No such host is known
  at System.Net.Dns.hostent_to_IPHostEntry (System.String h_name, System.String[] h_aliases, System.String[] h_addrlist) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostByName (System.String hostName) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostEntry (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
  at System.Net.Dns.GetHostAddresses (System.String hostNameOrAddress) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpClient.Connect (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
  at System.Net.Sockets.TcpClient..ctor (System.String hostname, Int32 port) [0x00000] in <filename unknown>:0
  at Novell.Directory.Ldap.Connection.connect (System.String host, Int32 port, Int32 semaphoreId) [0x00000] in <filename unknown>:0

很明显,它试图查找一个它找不到的主机。我的windows和linux系统都使用相同的dns服务器,所以这并不是造成问题的原因。

当windows上的跟踪正常工作时,我阅读了它--跟踪显示,查找失败了几次,自动发现方法尝试了几个不同的urls,直到它击中了一个工作正常的urls-但是,在第一个失败之后,它似乎就会掉下来,这就是它的结束。

我已经尝试谷歌使用新闻在mono上,但我还没有找到谁正在这样做,所以我真的不知道还可以尝试什么。

所使用的代码如下所示--几乎所有这些代码都取自http://msdn.microsoft.com/en-us/library/exchange/dd633709(v=exchg.80).aspx上的代码示例。

代码语言:javascript
复制
class Program
{
    private static int verbose = 10;
    private static string loginEmail = "email@example.com";
    private static string password = "#############";

    static void Main(string[] args)
    {
        try
        {

            ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack;

            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

            service.Credentials = new WebCredentials(loginEmail, password);

            if (verbose >= 10)
            {

                service.TraceEnabled = true;
                service.TraceFlags = TraceFlags.All;

            }

            service.AutodiscoverUrl(loginEmail, RedirectionUrlValidationCallback);

            Console.WriteLine("AutoDiscover Completed");

            getContacts(service);

            Console.ReadLine();

        }
        catch (Exception e) {
            Console.WriteLine(e.Message);
            foreach (string key in e.Data.Keys)
            {
                Console.WriteLine(String.Format("{0}: {1}",key, e.Data[key]));
            }
            throw e;
        }

    }

    private static void getContacts(ExchangeService service){


        // Get the number of items in the Contacts folder.
        ContactsFolder contactsfolder = ContactsFolder.Bind(service, WellKnownFolderName.Contacts);

        // Set the number of items to the number of items in the Contacts folder or 1000, whichever is smaller.
        int numItems = contactsfolder.TotalCount < 1000 ? contactsfolder.TotalCount : 1000;

        // Instantiate the item view with the number of items to retrieve from the Contacts folder.
        ItemView view = new ItemView(numItems);

        // To keep the request smaller, request only the display name property.
        //view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ContactSchema.DisplayName);

        // Retrieve the items in the Contacts folder that have the properties that you selected.
        FindItemsResults<Item> contactItems = service.FindItems(WellKnownFolderName.Contacts, view);

        // Display the list of contacts. 
        foreach (Item item in contactItems)
        {
            if (item is Contact)
            {
                Contact contact = item as Contact;

                Console.WriteLine();
                Console.WriteLine(contact.DisplayName);
                if (verbose >= 2)
                {
                    Console.WriteLine("    " + contact.Id);
                }

                try
                {
                    Console.WriteLine("    " + contact.EmailAddresses[EmailAddressKey.EmailAddress1].ToString());
                }
                catch (Exception e) 
                {
                    if (verbose >= 5)
                    {
                        Console.WriteLine("    " + "Email Address 1 Not Available : " + e.Message);
                    }
                }
            }
        }

    }

    #region taken from tutorial

    private static bool CertificateValidationCallBack(
        object sender,
        System.Security.Cryptography.X509Certificates.X509Certificate certificate,
        System.Security.Cryptography.X509Certificates.X509Chain chain,
        System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }

        // If there are errors in the certificate chain, look at each error to determine the cause.
        if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
        {
            if (chain != null && chain.ChainStatus != null)
            {
                foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                {
                    if ((certificate.Subject == certificate.Issuer) &&
                       (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                    {
                        // Self-signed certificates with an untrusted root are valid. 
                        continue;
                    }
                    else
                    {
                        if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                        {
                            // If there are any other errors in the certificate chain, the certificate is invalid,
                            // so the method returns false.
                            return false;
                        }
                    }
                }
            }

            // When processing reaches this line, the only errors in the certificate chain are 
            // untrusted root errors for self-signed certificates. These certificates are valid
            // for default Exchange server installations, so return true.
            return true;
        }
        else
        {
            // In all other cases, return false.
            return false;
        }
    }

    private static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }
        return result;
    }

    #endregion

}

BeepBeep的回答帮助我解决了这个问题。

在使用BeepBeep的建议之后,我遇到了一个问题,即Mono似乎没有dnsapi.dll (根据异常)。我现在跳过自动发现来解决这个问题。

为了做到这一点,我替换了

代码语言:javascript
复制
service.AutodiscoverUrl(loginEmail, RedirectionUrlValidationCallback);

使用

代码语言:javascript
复制
service.Url = new Uri("https://blah.com/ews/exchange.asmx");

然后,我在证书上出现了一个错误(异常说的是'error with或decryption‘之类的东西)--这足以说明,默认情况下,mono不包括任何根ca证书,这里有更多信息:关于安全的单一常见问题

我选择了更懒惰的方式来获得我想要的证书,使用mozroot工具。然而,这并不像预期的那样工作,而且错误仍然存在。

然后,我使用了上述常见问题中的最近一个来确定问题--它与我正在使用的证书链有关(根被接受,但中间层没有被接受)。然后,我使用FAQ (certmgr)中记录的第三个工具来安装证书。

在那之后,一切都成功了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-08 09:27:18

使用以下代码解决了相同的问题:

代码语言:javascript
复制
ExchangeService service = new ExchangeService();
service.EnableScpLookup = false;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15279823

复制
相关文章

相似问题

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