我有一个简单的web服务调用,由。net (c#) 2.0 Windows应用程序生成,通过Visual Studio生成的web服务代理,用于同样用c#(2.0)编写的web服务。这种方法已经有效了好几年,并且在十几个正在运行的地方继续有效。

在新地点的新安装遇到了问题。当试图调用web服务时,它失败了,消息说:

无法为SSL/TLS安全建立信任关系 通道

web服务的URL使用SSL (https://)——但这已经在许多其他位置工作了很长时间(并继续这样做)。

我该往哪里看?这可能是Windows和。net之间的安全问题,是此安装独有的吗?如果是,我在哪里建立信任关系?我迷路了!


当前回答

如果你使用的是Windows 2003,你可以这样做:

Open Microsoft Management Console (Start --> Run --> mmc.exe); Choose File --> Add/Remove Snap-in; In the Standalone tab, choose Add; Choose the Certificates snap-in, and click Add; In the wizard, choose the Computer Account, and then choose Local Computer. Press Finish to end the wizard; Close the Add/Remove Snap-in dialog; Navigate to Certificates (Local Computer) and choose a store to import: If you have the Root CA certificate for the company that issued the certificate, choose Trusted Root Certification Authorities; If you have the certificate for the server itself, choose Other People Right-click the store and choose All Tasks --> Import Follow the wizard and provide the certificate file you have; After that, simply restart IIS and try calling the web service again.

参考:http://www.outsystems.com/NetworkForums/ViewTopic.aspx?Topic=Web-Services: -Could-not-establish-trust-relationship-for-the-SSL / TLS -…

其他回答

下面的代码片段将修复您正在调用的服务器上的SSL证书有问题的情况。例如,证书可能是自签名的,或者证书和服务器之间的主机名可能不匹配。

如果您正在调用您直接控制之外的服务器,这是危险的,因为您不能再确定您正在与您认为连接到的服务器通信。但是,如果您处理的是内部服务器,而获得“正确的”证书是不现实的,请使用以下命令告诉web服务忽略证书问题并勇敢地继续工作。

前两个使用lambda表达式,第三个使用常规代码。第一个接受任何证书。后两者至少检查证书中的主机名是否是您所期望的主机名。 ... 希望对你有帮助

//Trust all certificates
System.Net.ServicePointManager.ServerCertificateValidationCallback =
    ((sender, certificate, chain, sslPolicyErrors) => true);

// trust sender
System.Net.ServicePointManager.ServerCertificateValidationCallback
                = ((sender, cert, chain, errors) => cert.Subject.Contains("YourServerName"));

// validate cert by calling a function
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

// callback used to validate the certificate in an SSL conversation
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)
{
    bool result = cert.Subject.Contains("YourServerName");
    return result;
}

Luke写了一篇关于这方面的好文章。 非常直截了当。试试吧

卢克的解决方案

原因(引用自他的文章(含诅咒)) “. . 上面的代码的问题是,如果您的证书无效,它将无法工作。为什么我要发布到一个网页与无效的SSL证书?因为我很便宜,我不想付钱给Verisign或其他** *的人对我的测试箱进行认证,所以我自己签名了。当我发送请求时,我得到了一个可爱的例外:

System.Net.WebException 底层连接被关闭。无法与远程服务器建立信任关系。

我不知道你怎么想,但对我来说,这个异常看起来像是由我的代码中一个愚蠢的错误引起的,导致POST失败。所以我一直在寻找,调整,做各种奇怪的事情。只有在我谷歌了***n的东西,我发现遇到无效的SSL证书后的默认行为是抛出这个异常。 . .”

我有这个错误运行在一个web服务器的url如下:

a.b.domain.com

但是没有证书,所以我找了一个DNS

a_b.domain.com

在这里提示一下这个解因为它在谷歌中出现在最上面。

我刚刚遇到了这个问题。我的解决方案是通过手动同步时间服务器来更新系统时间。要做到这一点,你可以:

右键单击任务栏中的时钟 选择“调整日期/时间” 选择“Internet Time”选项卡 单击“更改设置” 选择立即更新

在我的情况下,这是不正确的同步,所以我必须点击它几次才能正确更新。如果它继续不正确地更新,您甚至可以尝试从服务器下拉菜单中使用不同的时间服务器。

微软的SSL诊断工具可能能够帮助识别问题。

更新链接已经修复。