gpt4 book ai didi

c# - 重置 ServerCertificateValidationCallback

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:33 33 4
gpt4 key购买 nike

我需要访问具有无效 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: Couldnot establish trust relationship for the SSL/TLS secure channel. --->System.Security.Authentication.AuthenticationException: The remotecertificate 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com