如何使用 CA 证书 C# 验证证书链

How to verify a certificate chain using a CA certificate C#

我正在尝试连接到 Mosquitto 代理。 经纪人将有一个 ca.crt 和一个 server.crt。我的应用程序将只有 ca.crt.

连接后,代理会同时提供 ca.crt 和 server.crt(证书链)。 我如何根据我已经拥有的 ca.crt 验证两者? ca.crt和客户端上的一样

使用 X509Chain class and put the ca.crt, loaded as X509Certificate2, onto the ExtraStore property of the ChainPolicy 属性.

var caCert = new X509Certificate2(".\ca.crt");
var serverCert = new X509Certificate2(".\server.crt");

X509Chain ch = new X509Chain();
ch.ChainPolicy.RevocationMode = X509RevocationMode.Online;
ch.ChainPolicy.ExtraStore = new X509Certificate2Collection(caCert);
ch.Build (serverCert);