2015-11-10 23 views
8

在我的應用程序中,我使用RestSharp來查詢REST API和System.Net.Mail來發送電子郵件。在程序啓動時,我設置了ServicePointManager.SecurityProtocol屬性。ServicePointManager SecurityProtocol衝突

如果我將該屬性設置爲:

The request was aborted: Could not create SSL/TLS secure channel 

如果我將該屬性設置爲:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11; 

例外是

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; 

異常與RestSharp查詢API時拋出與System.Net.Mail發送電子郵件時拋出:

System.Security.Authentication.AuthenticationException: A call to SSPI failed, see inner exception. ---> System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm 

我應該如何解決這個問題?

+0

什麼版本的.NET?如果你不是最新的,是否有可能升級?之後,我會嘗試縮小一點。使用chrome插件郵差嘗試與您的API進行通信。設置Fiddler(或wireshark)攔截流量並查看數據包以查看它正在嘗試協商的內容。你也只是使用標準的SmtpClient類?你的代碼是什麼樣的使用它? – Wjdavis5

+0

您可以在撥打電話之前爲服務點管理員設置正確的值。 –

回答

6

您連接到的REST API服務器和郵件服務器顯然具有衝突的安全協議要求。您需要爲他們使用不同的安全協議設置。

ServicePointManager.SecurityProtocol是靜態的,其當前值適用於所有新連接。很遺憾,沒有辦法根據ServicePoint來控制此設置。 (在我看來,這是微軟的一個設計缺陷)

如果你有對REST API服務器或郵件服務器的控制,那麼你可以重新配置它們來接受非衝突的安全協議。

否則,您可以重新設計代碼,以便與REST API和郵件服務器的所有連接都由兩個獨立的AppDomains組成。

例如,讓默認的應用程序域處理所有的REST API通信,併產生一個單獨的應用程序域來完成所有的郵件通信。

使用此設置,您可以在每個域中使用不同的ServicePointManager.SecurityProtocol值。 (因爲靜態值不在應用程序域之間共享)。