【问题标题】:ServicePointManager SecurityProtocol conflictServicePointManager 安全协议冲突
【发布时间】:2015-11-10 22:19:31
【问题描述】:

在我的应用程序中,我使用 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

我应该如何解决这个问题?

【问题讨论】:

  • 什么版本的 .NET?如果你不是最新的,是否可以升级?在那之后,我会开始尝试缩小范围。使用 chrome 插件 Postman 尝试与您的 API 进行通信。设置 Fiddler(或 wireshark)来拦截流量并查看数据包以查看它试图协商的内容。您还只是使用标准的 SmtpClient 类吗?使用它的代码是什么样的?
  • 您可以在拨打电话之前为服务点管理器设置正确的值。

标签: c# .net restsharp system.net.mail


【解决方案1】:

REST API 服务器和您要连接的邮件服务器显然具有冲突的安全协议要求。您需要为它们使用不同的安全协议设置。

ServicePointManager.SecurityProtocol 是静态的,其当前值适用于所有新连接。不幸的是,没有办法根据ServicePoint 控制此设置。 (在我看来这是微软的设计缺陷)

如果您可以控制 REST API 服务器或邮件服务器,那么您或许可以重新配置它们以接受不冲突的安全协议。

否则,您可以重新设计您的代码,以便与 REST API 和邮件服务器的所有连接都来自两个独立的 AppDomains

例如,让默认应用程序域处理所有 REST API 通信,并生成一个单独的应用程序域来处理所有邮件通信。

通过此设置,您可以在每个域中使用不同的ServicePointManager.SecurityProtocol 值。 (因为应用域之间不共享静态值)。

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多