2013-03-07 104 views
5

運行Exchange EWS我試圖與Debian的使用Exchange EWS 2通過單聲道(版本2.10.8.1 & 3.0.6) 我使用vs2012 Windows 8的開發試驗。單聲道LdapException

該程序在Windows上工作得很好,我得到了預期的輸出。

在單聲道,但我不斷收到以下輸出和異常。

<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上的痕跡看,當它工作 - 和跟蹤顯示查找失敗了幾次,自動發現方法嘗試了一些不同的URL,直到它擊中一個工程 - 單聲道但它似乎陷入第一次失敗後就結束了,這就結束了。

我試過在單聲道上使用ews的谷歌搜索,但我還沒有找到任何人這樣做,所以我真的不知道還有什麼要嘗試。

使用的代碼如下 - 幾乎所有的它是從代碼示例採取 http://msdn.microsoft.com/en-us/library/exchange/dd633709(v=exchg.80).aspx

class Program 
{ 
    private static int verbose = 10; 
    private static string loginEmail = "[email protected]"; 
    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的建議後,我有一個問題,單聲道似乎沒有dnsapi.dll(根據例外)。我現在通過跳過自動發現解決了這個問題。

爲了做到這一點,我換成

service.AutodiscoverUrl(loginEmail, RedirectionUrlValidationCallback); 

service.Url = new Uri("https://blah.com/ews/exchange.asmx"); 

然後,我有一個錯誤與證書(例外是這樣說:「有要求或解密錯誤」) - 足以說,你需要知道mono默認不包含任何root ca證書,更多信息在這裏:Mono FAQ about Security

我選擇了lazier的方式來ge t我想要的證書,使用mozroots工具。然而,這並沒有像預期的那樣工作,錯誤依然存在。

然後我使用tlstest也從上面的常見問題來確定問題 - 它與我使用的證書鏈(根被接受,但中間不被接受)有關。然後,我使用FAQ(certmgr)中記錄的第三個工具來安裝證書。

接下來,它一切正常。

+0

我想補充我是新來的都EWS和單 – m3z 2013-03-07 19:19:38

+0

我升級到單聲道3.0.6 - 同樣的問題 – m3z 2013-03-08 08:18:27

回答

2

同樣的問題,使用此代碼解決:

ExchangeService service = new ExchangeService(); 
service.EnableScpLookup = false; 
+0

感謝。我會試試這個。 – m3z 2013-04-09 10:29:58

+0

我認爲這讓我進入下一步。現在我必須弄清楚如何包含dnsapi。單聲道的dll。或者其他的東西。 ...關閉做一些閱讀我認爲 – m3z 2013-04-09 11:03:58

+0

謝謝你。它現在正在工作。我不得不做一些其他的東西,我要添加細節到我的問題,供他人蔘考,但你的小費給了我關鍵。 – m3z 2013-04-09 13:05:48