无法建立 SSL/TLS 安全通道的信任关系 -- SOAP
我有一个简单的 Web 服务调用,由 .NET (C#) 2.0 Windows 应用程序通过 Visual Studio 生成的 Web 服务代理生成,用于同样用 C# (2.0) 编写的 Web 服务。 这种做法已经行之有效了好几年,并且在十几个正在运行的地方继续如此。
新站点的新安装遇到了问题。 尝试调用 Web 服务时,失败并显示以下消息:
无法建立 SSL/TLS 安全信任关系 频道
Web 服务的 URL 使用 SSL (https://) ——但这已经在许多其他位置工作了很长时间(并且继续如此)。
我该往哪里看? 这可能是 Windows 和 .NET 之间的安全问题,是此安装特有的吗? 如果是这样,我在哪里建立信任关系? 我迷路了!
I have a simple web service call, generated by a .NET (C#) 2.0 Windows app, via the web service proxy generated by Visual Studio, for a web service also written in C# (2.0). This has worked for several years, and continues to do so at the dozen or so places where it is running.
A new installation at a new site is running into a problem. When attempting to invoke the web service, it fails with the message saying:
Could not establish a trust relationship for the SSL/TLS secure
channel
The URL of the web service uses SSL (https://) -- but this has been working for a long time (and continues to do so) from many other locations.
Where do I look? Could this be a security issue between Windows and .NET that is unique to this install? If so, where do I set up trust relationships? I'm lost!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
以下代码片段将修复您调用的服务器上的 SSL 证书出现问题的情况。 例如,它可能是自签名的,或者证书和服务器之间的主机名可能不匹配。
如果您正在调用超出您直接控制范围的服务器,这很危险,因为您无法再确定正在与您认为已连接的服务器进行通信。 但是,如果您正在处理内部服务器并且获得“正确”的证书不切实际,请使用以下命令告诉 Web 服务忽略证书问题并勇敢地继续下去。
前两个使用 lambda 表达式,第三个使用常规代码。 第一个接受任何证书。 最后两项至少检查证书中的主机名是否是您期望的主机名。
...希望您觉得它有帮助
The following snippets will fix the case where there is something wrong with the SSL certificate on the server you are calling. For example, it may be self-signed or the host name between the certificate and the server may not match.
This is dangerous if you are calling a server outside of your direct control, since you can no longer be as sure that you are talking to the server you think you're connected to. However, if you are dealing with internal servers and getting a "correct" certificate is not practical, use the following to tell the web service to ignore the certificate problems and bravely soldier on.
The first two use lambda expressions, the third uses regular code. The first accepts any certificate. The last two at least check that the host name in the certificate is the one you expect.
... hope you find it helpful
非常简单的“包罗万象”解决方案是这样的:
sebastian-castaldi 的解决方案更详细一些。
The very simple "catch all" solution is this:
The solution from sebastian-castaldi is a bit more detailed.
想法(基于过去的痛苦):
服务器机器的时钟设置是否正确(即UTC时间是正确的[忽略本地时间,这在很大程度上是无关紧要的]) - 这对于WCF来说当然很重要,因此可能会影响常规的SOAP?Thoughts (based on pain in the past):
servermachine have the clock set correctly (i.e. so that the UTC time is correct [ignore local time, it is largely irrelevent]) - this certainly matters for WCF, so may impact regular SOAP?我个人最喜欢以下解决方案:
...然后在请求收到错误之前,执行以下操作
在咨询卢克的解决方案
I personally like the following solution the most:
... then before you do request getting the error, do the following
Found this after consulting Luke's Solution
如果您不想盲目信任每个人并仅对某些主机设置信任例外,则以下解决方案更合适。
然后,只需在应用程序启动时调用 Ssl.EnableTrustedHosts 即可。
If you do not wan't to blindly trust everybody and make a trust exception only for certain hosts the following solution is more appropriate.
Then just call Ssl.EnableTrustedHosts when your app starts.
如果您使用的是 Windows 2003,您可以尝试以下操作:
参考: http://www.outsystems.com/NetworkForums/ViewTopic.aspx?Topic=Web-Services:-Could-not-built-trust-relationship-for-the-SSL/TLS- ...
If you are using Windows 2003, you can try this:
Reference: http://www.outsystems.com/NetworkForums/ViewTopic.aspx?Topic=Web-Services:-Could-not-establish-trust-relationship-for-the-SSL/TLS-...
卢克(Luke)就此写了一篇非常好的文章..
非常简单..尝试一下
卢克的解决方案
原因(引自他的文章(减去咒骂))
“..
上面代码的问题是,如果您的证书无效,它就不起作用。 为什么我要发布到带有无效 SSL 证书的网页? 因为我很便宜,而且我不想向 Verisign 或其他 **-* 之一支付我的测试盒证书,所以我自行签署了它。 当我发送请求时,我收到一个可爱的异常:
System.Net.WebException
底层连接已关闭。 无法与远程服务器建立信任关系。
我不了解您的情况,但对我来说,该异常看起来像是由我的代码中的一个愚蠢错误引起的,导致 POST 失败。 所以我一直在寻找、调整、做各种奇怪的事情。 只有在我用谷歌搜索了 ***n 的事情之后,我才发现遇到无效的 SSL 证书后的默认行为是抛出这个异常。
..”
Luke wrote a pretty good article about this ..
pretty straight forward .. give this a try
Luke's Solution
Reason (quote from his article (minus cursing))
"..
The problem with the code above is that it doesn’t work if your certificate is not valid. Why would I be posting to a web page with and invalid SSL certificate? Because I’m cheap and I didn’t feel like paying Verisign or one of the other **-*s for a cert to my test box so I self signed it. When I sent the request I got a lovely exception thrown at me:
System.Net.WebException
The underlying connection was closed. Could not establish trust relationship with remote server.
I don’t know about you, but to me that exception looked like something that would be caused by a silly mistake in my code that was causing the POST to fail. So I kept searching, and tweaking and doing all kinds of weird things. Only after I googled the ***n thing I found out that the default behavior after encountering an invalid SSL cert is to throw this very exception.
.."
Microsoft 的 SSL 诊断工具也许能够帮助确定问题。
更新链接现已修复。
Microsoft's SSL Diagnostics Tool may be able to help identify the issue.
UPDATE the link has been fixed now.
添加以下内容:
在您调用服务的行之前
add this:
right before the line that you're calling the service
我刚刚遇到这个问题。 我的解决方案是通过手动同步到时间服务器来更新系统时间。 为此,您可以:
调整日期/时间
Internet时间
选项卡更改设置
立即更新
在我的例子中,同步不正确,因此我必须单击它几次才能正确更新。 如果它继续错误更新,您甚至可以尝试使用服务器下拉列表中的不同时间服务器。
I just encountered this issue. My resolution was to update the system time by manually syncing to the time servers. To do this you can:
Adjust Date/Time
Internet Time
tabChange Settings
Update Now
In my case this was syncing incorrectly so I had to click it several times before it updated correctly. If it continues to update incorrectly you can even try using a different time server from the server drop-down.
尝试一下:
请注意,您至少必须使用 4.5 .NET 框架
Try this:
Notice that you have to work at least with 4.5 .NET framework
我在 Internet Explorer 中的
.NET
应用程序中遇到了类似的问题。我解决了将证书(在我的例子中为 VeriSign 3 类证书)添加到受信任的编辑器证书中的问题。
如果您导出证书即可获得:
谢谢
I had a similar problem in
.NET
app in Internet Explorer.I solved the problem adding the certificate (VeriSign Class 3 certificate in my case) to trusted editors certificates.
You can get the certificate if you export it from:
thanks
我在一个网络服务器上运行时遇到了这个错误,其网址如下:
但没有证书,所以我得到了一个名为“
Just puthinttothissolutionhere”的 DNS,因为这在谷歌中排名最高。
I had this error running against a webserver with url like:
but there was no certificate for it, so I got a DNS called
Just putting hint to this solution here since this came up top in google.
对于那些通过 VS 客户端遇到此问题的人,一旦成功添加了服务引用并尝试执行第一个调用,就会出现此异常:
“底层连接已关闭:无法为 SSL/TLS 安全通道建立信任关系”
如果您使用(如我的情况)带有 IP 地址的端点 URL 并遇到此异常,那么您可能需要执行以下步骤重新添加服务引用:
再试一次 :)。
谢谢
For those who are having this issue through a VS client side once successfully added a service reference and trying to execute the first call got this exception:
“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel”
If you are using (like my case) an endpoint URL with the IP address and got this exception, then you should probably need to re-add the service reference doing this steps:
Try again :).
Thanks
就我而言,我尝试使用 IIS 7 在 Visual Studio 环境中测试 SSL。
这就是我最终所做的使其正常工作:
在我的网站下的“绑定..”在 IIS 右侧的“绑定...”部分中,我必须将“https”绑定添加到端口 443 并选择“IIS Express 开发证书”。
在我的网站右侧的“高级设置...”部分中,我必须将“启用的协议”从“http”更改为“https”。
在“SSL 设置”图标下,我为客户端证书选择了“接受”。
然后我必须回收应用程序池。
我还必须使用 mmc.exe 将本地主机证书导入到我的个人存储中。
我的
web.config
文件已经正确配置,所以在我整理完以上所有内容后,我就可以继续我的测试了。In my case I was trying to test SSL in my Visual Studio environment using IIS 7.
This is what I ended up doing to get it to work:
Under my site in the 'Bindings...' section on the right in IIS, I had to add the 'https' binding to port 443 and select "IIS Express Developement Certificate".
Under my site in the 'Advanced Settings...' section on the right I had to change the 'Enabled Protocols' from "http" to "https".
Under the 'SSL Settings' icon I selected 'Accept' for client certificates.
Then I had to recycle the app pool.
I also had to import the local host certificate into my personal store using mmc.exe.
My
web.config
file was already configured correctly, so after I got all the above sorted out, I was able to continue my testing.我的解决方案(VB.Net,此应用程序的“临时”(UAT)版本需要使用“临时”证书,但一旦请求位于实时站点上,就不会影响请求):
My solution (VB.Net, the "staging" (UAT) version of this application needs to work with the "staging" certificate but not affect requests once they are on the live site):
我已经使用了一段时间的一个变体,如果对任何人有帮助的话。
调用者必须明确请求需要不受信任的认证,并在完成后将回调放回到默认状态。
A variation I've been using for a while it if helps anyone.
The caller has to explicitly request that untrusted certifications are required and places the callback back into it's default state upon completion.
如果证书不工作,当ServerCertificateValidationCallback返回true;
我的 ServerCertificateValidationCallback 代码:
我的代码被阻止执行 ServerCertificateValidationCallback:
OnValidateCertificateError 函数:
我禁用了 CertificateValidation 代码并且 ServerCertificateValidationCallback 运行得很好
If not work bad sertificate, when ServerCertificateValidationCallback return true;
My ServerCertificateValidationCallback code:
My code which the prevented execute ServerCertificateValidationCallback:
OnValidateCertificateError function:
I disabled CertificateValidation code and ServerCertificateValidationCallback running very well