c# - 重置 ServerCertificateValidationCallback

标签 c# .net ssl ssl-certificate

我需要访问具有无效 SSL 证书的 REST 服务。所以我在我的代码中添加了以下内容:

 System.Net.ServicePointManager.ServerCertificateValidationCallback = 
    ((sender, cert, chain, errors) =>
       cert.Subject.Contains("soap.example.com"));

之后,我执行我必须执行的操作以将请求发送到 REST 服务。

一切都很好。

但稍后我需要连接到不同的域(具有有效的 SSL 证书)。后者由于证书错误而失败,如果我重新启动 IIS,该错误就会消失,并且只会在再次调用上面显示的代码段后返回:

System.Net.WebException: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. ---> System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.

那么在将请求发送到带有无效证书的 REST 服务后,我如何才能确保正常行为再次起作用?

简单地重置为 ServerCertificateValidationCallback=null 似乎不起作用。

(我完全了解 CallBack 的危险)

最佳答案

System.Net.ServicePointManager.ServerCertificateValidationCallback = 
        ((sender, cert, chain, errors) => 
        errors == SslPolicyErrors.None || cert.Subject.Contains("soap.example.com"));

应该可以解决问题。

关于c# - 重置 ServerCertificateValidationCallback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44802884/

相关文章:

c# - 为什么 Int32.MaxValue * Int32.MaxValue == 1?

c# - 具有依赖注入(inject)的 Razor 助手

c# - CAPICOM - 验证 SignedCode 是否来自没有 UI 的可信发布者

.net - swagger.json 不会根据部署时的代码更改进行更新

java - 证书签名验证失败

vb.net - 使 Selenium Firefox Webdriver 信任根证书

c# - 这是一个好的 REST URI 吗?

c# - 不使用凭据 C# 发送邮件

.net - 如何在Web网格的列中编写IF条件

qt - 这是使用 QWebPage 设置 SSL 协议(protocol)的正确方法吗?