ServicePointManager 安全协议冲突
ServicePointManager SecurityProtocol conflict
在我的应用程序中,我使用 RestSharp 查询 REST API 并使用 System.Net.Mail 发送电子邮件。在程序启动时,我设置了 ServicePointManager.SecurityProtocol 属性.
如果我将 属性 设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
使用 RestSharp 查询 API 时抛出异常:
The request was aborted: Could not create SSL/TLS secure channel
如果我将 属性 设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
使用 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
我该如何解决这个问题?
REST API 服务器和您要连接的邮件服务器显然具有冲突的安全协议要求。您需要为它们使用不同的安全协议设置。
ServicePointManager.SecurityProtocol is static and its current value applies to all new connections. There is unfortunately no way to control this setting per ServicePoint。 (我认为这是微软的设计缺陷)
如果您可以控制 REST API 服务器或邮件服务器,那么您或许可以重新配置它们以接受不冲突的安全协议。
否则,您可以重新设计代码,以便所有与 REST API 和邮件服务器的连接都由两个单独的 AppDomains。
例如,让默认应用程序域处理所有 REST API 通信并生成一个单独的应用程序域来执行所有邮件通信。
通过此设置,您可以在每个域中使用不同的 ServicePointManager.SecurityProtocol 值。 (因为应用程序域之间不共享静态值)。
在我的应用程序中,我使用 RestSharp 查询 REST API 并使用 System.Net.Mail 发送电子邮件。在程序启动时,我设置了 ServicePointManager.SecurityProtocol 属性.
如果我将 属性 设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
使用 RestSharp 查询 API 时抛出异常:
The request was aborted: Could not create SSL/TLS secure channel
如果我将 属性 设置为:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
使用 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
我该如何解决这个问题?
REST API 服务器和您要连接的邮件服务器显然具有冲突的安全协议要求。您需要为它们使用不同的安全协议设置。
ServicePointManager.SecurityProtocol is static and its current value applies to all new connections. There is unfortunately no way to control this setting per ServicePoint。 (我认为这是微软的设计缺陷)
如果您可以控制 REST API 服务器或邮件服务器,那么您或许可以重新配置它们以接受不冲突的安全协议。
否则,您可以重新设计代码,以便所有与 REST API 和邮件服务器的连接都由两个单独的 AppDomains。
例如,让默认应用程序域处理所有 REST API 通信并生成一个单独的应用程序域来执行所有邮件通信。
通过此设置,您可以在每个域中使用不同的 ServicePointManager.SecurityProtocol 值。 (因为应用程序域之间不共享静态值)。