2013-03-07 10 views
5

Ich versuche mit Exchange EWS 2 debian über Mono (Version 2.10.8.1 & 3.0.6) experimentieren Ich entwickle auf Windows 8 mit vs2012.Ausführen von Exchange EWS auf Mono LdapException

Das Programm funktioniert einwandfrei auf Windows und ich bekomme die erwartete Ausgabe.

Auf Mono jedoch bekomme ich immer die folgende Ausgabe und Ausnahme.

<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 

Anscheinend versucht es, einen Wirt nachzuschlagen, den er nicht finden kann. Beide meine Windows und Linux-Systeme verwenden den gleichen DNS-Server, so dass es nicht das Problem verursacht.

Ich lese die Ablaufverfolgung auf Windows, wenn es funktioniert - und die Ablaufverfolgung zeigt, dass die Nachschlagevorgänge ein paar Mal fehlschlagen und die Autodiscover-Methode versucht ein paar verschiedene URLs, bis es eine trifft, die funktioniert - auf Mono jedoch scheint es zu fallen vorbei nach dem ersten Fehler und das ist das Ende davon.

Ich habe versucht googeln für die Verwendung von ews auf Mono, aber ich habe niemanden gefunden, der es tut, also bin ich nicht wirklich sicher, was ich noch versuchen sollte.

Der verwendete Code ist unten - so ziemlich alles davon aus Code-Beispiele auf 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 

} 

Die Antwort von BeepBeep hat mir geholfen, dieses Problem zu lösen genommen wird.

Nach BeepBeeps Vorschlag hatte ich dann ein Problem, dass Mono erschien dnsapi.dll (nach den Ausnahmen) nicht zu haben. Ich habe das Problem gelöst, indem ich die AutoErmittlung jetzt überspringe.

Um das zu tun, ersetzt i

service.AutodiscoverUrl(loginEmail, RedirectionUrlValidationCallback); 

mit

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

Dann hatte ich einen Fehler mit dem Zertifikat (die Ausnahme, sagte etwas wie ‚Fehler mit Anfrage oder Entschlüsselung‘) - Genug zu sagen, Sie müssen wissen, dass mono standardmäßig keine Root-CA-Zertifikate enthält, weitere Informationen finden Sie hier: Mono FAQ about Security

Ich wählte den faulen Weg zu ge t die certs ich wollte, mit mozroots tool. Dies funktionierte jedoch nicht wie erwartet und der Fehler blieb bestehen.

Dann habe ich den Tlstest auch von den oben genannten FAQ verwendet, um das Problem zu bestimmen - es war mit der Zertifikatskette verwandt, die ich verwendete (die Wurzel wurde akzeptiert, aber das Zwischenprodukt wurde nicht akzeptiert). Ich habe dann das dritte in den FAQ (certmgr) dokumentierte Tool verwendet, um die Zertifikate zu installieren.

Danach funktioniert alles.

+0

ich hinzufügen möchte ich sowohl ews und mono bin neu – m3z

+0

Ich habe ein Upgrade 3.0.6 auf Mono - gleiche Problem – m3z

Antwort

2

gleiche Problem und gelöst mit diesem Code:

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

Dank. Ich werde es versuchen. – m3z

+0

Ich denke, es hat mich zum nächsten Schritt gebracht. Jetzt muss ich herausfinden, wie man dnsapi einbaut.dll in Mono. oder so. ... Off, um etwas zu lesen, denke ich – m3z

+0

Vielen Dank. Es funktioniert jetzt. Ich musste ein paar andere Sachen machen, die ich meiner Frage für die Referenz von anderen hinzufügen werde, aber dein Tipp gab mir den Schlüssel. – m3z