来自表单的 HTTPS Web 服务调用有效,而来自 IIS 内部的无效

HTTPS web service call from form works and from inside IIS doesn't

我有一个 Web 应用程序,其中一个进程需要调用外部服务器中的 https Web 服务。

当我打电话时,我得到了这个异常:An error occurred while making the HTTP request to https://servicos.portaldasfinancas.gov.pt:701/sgdtws/documentosTransporte. This could be due to the fact that the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.

内部异常是:The underlying connection was closed: An unexpected error occurred on a send.

为了调试,我创建了一个 windows 表单应用程序,它使用完全相同的代码并执行完全相同的调用,并为调用访问了相同的证书。但是在 windows 表单应用程序上,调用没有问题。

我不知道是什么原因导致 IIS 内部的连接失败,也不知道两者之间有什么不同。

我确定了问题并设法找到了解决方案。

问题在于代码使用的是已登录用户可以访问但 IIS 没有访问权限的证书。为了以 IIS 可以访问它的方式安装证书,您需要:

  1. 在 MMC.exe 添加本地计算机证书存储视图
  2. 在该商店的个人 > 证书上安装证书
  3. 安装证书时选中可导出选项
  4. 安装证书后确认整个证书链是可信的。如果链中的 link 显示红叉表示它不受信任,请将该证书复制到受信任的根证书颁发机构文件夹。
  5. 重启 IIS 以防万一