C# 从文件中链接证书

C# 从文件中链接证书,c#,x509certificate2,C#,X509certificate2,我对证书验证有问题。我有一个.perm文件,它是链式证书文件(里面有多个开始和结束证书) 我尝试导入证书集合,但导入后集合的长度为1 X509Certificate2Collection collection = new X509Certificate2Collection(); collection.Import(certpath); 我看不到任何有趣的选择 X509Chain chain2 = new X509Chain(); 我得到的验证返回值为false,我认为原因是并非所有证书都已

我对证书验证有问题。我有一个.perm文件,它是链式证书文件(里面有多个开始和结束证书)

我尝试导入证书集合,但导入后集合的长度为1

X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certpath);
我看不到任何有趣的选择

X509Chain chain2 = new X509Chain();
我得到的验证返回值为false,我认为原因是并非所有证书都已加载

下面是我的完整验证方法

    private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        try
        {
            string certpath = "actual path";

            X509Certificate2Collection collection = new X509Certificate2Collection();
            collection.Import(certpath);

            X509Chain chain2 = new X509Chain();
            foreach(X509Certificate2 c in collection)
            {
                chain2.ChainPolicy.ExtraStore.Add(c);
            }

            // Check all properties
            chain2.ChainPolicy.VerificationFlags = X509VerificationFlags.NoFlag;

            // This setup does not have revocation information
            chain2.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;

            // Build the chain
            chain2.Build(new X509Certificate2(certificate));

            // Are there any failures from building the chain?
            if (chain2.ChainStatus.Length == 0)
                return true;

            // If there is a status, verify the status is NoError
            bool result = chain2.ChainStatus[0].Status == X509ChainStatusFlags.NoError;

            return result;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
        return false;
    }

X509Certificate2Collection
Import
方法不支持包含多个证书(一个接一个附加)的文件。有关此方法,请参阅文档

有一种格式可能有效——
SerializedStore
,但文档中并未对此进行详细说明。我假设它是
SerializedCert
中的某个国王,它是一个具有其属性的证书,因此即使这种格式也与您拥有的不匹配


尝试分离证书并使用初始化
X509Certificate2Collection

我已编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。