2013-03-07 27 views
5

Tôi đang thử nghiệm bằng cách sử dụng Exchange EWS 2 trên debian qua Mono (Phiên bản 2.10.8.1 & 3.0.6) Tôi đang phát triển trên windows 8 sử dụng vs2012.Chạy Exchange EWS trên Mono LdapException

Chương trình hoạt động tốt trên các cửa sổ và tôi nhận được kết quả mong đợi.

Tuy nhiên, tôi vẫn nhận được kết quả và ngoại lệ sau.

<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 

Có vẻ như nó đang tìm kiếm một máy chủ không thể tìm thấy. Cả hai cửa sổ và hệ thống Linux của tôi đang sử dụng cùng một máy chủ dns để nó không phải là gây ra vấn đề.

Tôi đọc qua dấu vết trên cửa sổ khi nó hoạt động - và dấu vết cho thấy tra cứu thất bại vài lần và phương pháp tự động phát hiện một số url khác nhau cho đến khi nó chạm vào sau khi thất bại đầu tiên và đó là kết thúc của nó.

Tôi đã cố gắng googling để sử dụng ews trên mono nhưng tôi đã không tìm thấy bất cứ ai đang làm điều đó vì vậy tôi không thực sự chắc chắn những gì khác để thử.

Mã được sử dụng dưới - khá nhiều tất cả của nó được lấy từ ví dụ mã trên 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 

} 

Câu trả lời từ BeepBeep giúp tôi giải quyết việc này.

Sau khi sử dụng đề xuất của BeepBeep, tôi đã gặp sự cố là Mono dường như không có dnsapi.dll (theo các trường hợp ngoại lệ). Tôi đã giải quyết vấn đề này bằng cách bỏ qua tự động phát hiện ngay bây giờ.

Để làm điều đó, tôi thay thế

service.AutodiscoverUrl(loginEmail, RedirectionUrlValidationCallback); 

với

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

Sau đó, tôi đã có một lỗi với chứng chỉ (ngoại trừ cho biết một cái gì đó như 'lỗi với yêu cầu hoặc giải mã') - đủ để nói, bạn cần phải biết rằng mono không bao gồm bất kỳ chứng chỉ ca gốc nào theo mặc định, thêm thông tin tại đây: Mono FAQ about Security

Tôi chọn cách lười hơn để t các certs tôi muốn, sử dụng công cụ mozroots. Tuy nhiên, điều này không hoạt động như mong đợi và lỗi vẫn tồn tại.

Sau đó tôi sử dụng tlstest cũng từ Câu hỏi thường gặp ở trên để xác định sự cố - nó liên quan đến chuỗi chứng chỉ tôi đang sử dụng (gốc đã được chấp nhận, nhưng trung gian không được chấp nhận). Sau đó tôi đã sử dụng công cụ thứ ba được ghi trong FAQ (certmgr) để cài đặt các chứng chỉ.

Sau đó, tất cả đều hoạt động.

+0

tôi có thể thêm tôi mới vào cả EWS và mono – m3z

+0

tôi nâng cấp lên mono 3.0.6 - cùng một vấn đề – m3z

Trả lời

2

Cùng một vấn đề và giải quyết sử dụng mã này:

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

Cảm ơn. Tôi sẽ thử. – m3z

+0

Tôi nghĩ rằng nó đã đưa tôi đến bước tiếp theo. Bây giờ tôi phải làm việc ra làm thế nào để bao gồm dnsapi.dll in mono. hay gì đó. ... Off để làm một số đọc tôi nghĩ rằng – m3z

+0

Cảm ơn bạn. Nó hiện đang hoạt động. Tôi đã phải làm một số công cụ khác, mà tôi sẽ thêm chi tiết vào câu hỏi của tôi để tham khảo khác nhưng mẹo của bạn đã cho tôi chìa khóa. – m3z

Các vấn đề liên quan