重置 ServerCertificateValidationCallback
Resetting ServerCertificateValidationCallback
我需要访问具有无效 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
似乎不起作用。
(我完全了解回调的危险)
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, cert, chain, errors) =>
errors == SslPolicyErrors.None || cert.Subject.Contains("soap.example.com"));
应该可以解决问题。
我需要访问具有无效 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
似乎不起作用。
(我完全了解回调的危险)
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((sender, cert, chain, errors) =>
errors == SslPolicyErrors.None || cert.Subject.Contains("soap.example.com"));
应该可以解决问题。