在.Net中调用Web服务时,绕过无效的SSL证书错误

jan*_*rgh 87 .net sharepoint web-services

我们正在设置一个新的SharePoint,但我们还没有有效的SSL证书.我想在其上调用Lists Web服务来检索有关设置的一些元数据.但是,当我尝试这样做时,我得到了例外:

底层连接已关闭:无法为SSL/TLS安全通道建立信任关系.

嵌套异常包含错误消息:

根据验证程序,远程证书无效.

这是正确的,因为我们使用的是临时证书.

我的问题是:如何告诉.Net Web服务客户端(SoapHttpClientProtocol)忽略这些错误?

小智 111

或者,您可以注册一个忽略认证错误的回叫代理:

...
ServicePointManager.ServerCertificateValidationCallback = MyCertHandler;
...

static bool MyCertHandler(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors error)
{
// Ignore errors
return true;
}
Run Code Online (Sandbox Code Playgroud)

  • 这就是你需要做的所有事情,如果你想对*"中间人"*攻击感到害怕...... (4认同)
  • 理想情况下,您应该只在开发环境中执行此操作。 (2认同)

Kei*_*ons 79

就像Jason S的回答一样:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Run Code Online (Sandbox Code Playgroud)

我把它放在我的Main中并查看我的app.config测试是否(ConfigurationManager.AppSettings["IgnoreSSLCertificates"] == "True")在调用该行代码之前.


Ima*_*idi 24

我这样解决了:

在调用导致该错误的ssl webservice之前调用以下内容:

using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

/// <summary>
/// solution for exception
/// 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.
/// </summary>
public static void BypassCertificateError()
{
    ServicePointManager.ServerCertificateValidationCallback +=

        delegate(
            Object sender1,
            X509Certificate certificate,
            X509Chain chain,
            SslPolicyErrors sslPolicyErrors)
        {
            return true;
        };
}
Run Code Online (Sandbox Code Playgroud)


Sim*_*son 18

我遇到此问题时使用的方法是将临时证书的签名者添加到相关计算机上的受信任机构列表中.

我通常使用CACERT创建的证书进行测试,并将它们添加到我信任的权限列表中.

这样做意味着您不必向应用程序添加任何自定义代码,它可以正确模拟部署应用程序时会发生的情况.因此,我认为这是以编程方式关闭检查的优秀解决方案.


Din*_*jan 11

我使用DownloadString时遇到了同样的错误; 并能够使其工作如下,并在此页面上提出建议

System.Net.WebClient client = new System.Net.WebClient();            
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
string sHttpResonse = client.DownloadString(sUrl);
Run Code Online (Sandbox Code Playgroud)