解决 dotnet restore NuGet 报证书错误的问题

发布于 2020-02-17  6158 次阅读


刚给服务器装好 dotnet 准备跑起来,发现 NuGet 竟然闹幺蛾子了:

> dotnet restore
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/www/dotnet/xxx/xxx.csproj]
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error :   The SSL connection could not be established, see inner exception. [/www/dotnet/xxx/xxx.csproj]
/usr/share/dotnet/sdk/3.1.101/NuGet.targets(123,5): error :   The remote certificate is invalid according to the validation procedure. [/www/dotnet/xxx/xxx.csproj]

从提示信息来看,应该是根 CA 证书有问题,但是用 update-ca-trust 后也没用,OpenSSL 版本也不老,再用 openssh version -a 检查一下证书路径也没问题。绝望之时搜到 GitHub 上一个讨论:
https://github.com/NuGet/Home/issues/8169#issuecomment-497591809

I found it was caused by my wrong configuration of SSL CA certificate path.
After I update openssl to 1.1.1, the CA path is modified to "/usr/local/ssl/certs", but it is an empty folder. Before updating openssl, the old version points to "/usr/lib/ssl/certs". After I manually link "/usr/local/ssl/certs" to /etc/ssl/certs. This error disappears.

检查了一下,发现好像有两个 OpenSSL……咱也不知道咋回事,用的是阿里云 CentOS 带 BT 面板的镜像,真的在/usr/local/openssl/这里还有一个,dotnet 用的应该是这里的,点开 certs 文件夹一看,果然是空的,所以我们把这个文件夹删了,然后建一个软链接到正确的证书目录:

ln -s /etc/ssl/certs /usr/local/openssl/certs

成功!

如果你也有这样的问题,记得到目录里找找是不是由于空的 certs 文件夹导致的。当然如果像我这样是有两个 OpenSSL 的奇葩情况,最好,emmm,重装一下?

Enjoy~


2021/06/02 更新:

这两天迁移了服务器,跑程序起来,其中有 HttpClient 碰到了 System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure.,然后在网上找了半天也解决不了,准备加个 CustomHandler,直接忽略掉所有证书判断了事,结果在编译的时候 nuget 报错,对,就是本文的同一个错误,我就很郁闷,看来糊一下是不行了,得彻底解决这个问题。

上谷歌搜,一搜,排名第二的,就是这篇文章……光速解决……被自己蠢哭了,自己记录下的东西还是要经常复习啊(语重心长)。

顺便再吐槽一下宝塔面板镜像,这次用的是腾讯云,没想到也有这样的问题,估计很少有人跑 dotnet 程序,所以没人发现吗……


本文被以下文章引用,其他作者也遇到了类似情况有不同的具体解决方法,可以参考:
Unable to load the service index for source 排错过程分享 - 云+社区 - 腾讯云


寻找属于自己的1%