-1

我正在使用以下代码在 C# Web API 中配置 SSL 证书。问题是我使用序列号查找 SSL 证书,每次 Infra 重新颁发 SSL 证书时,我都必须更新应用程序配置中的证书序列号。重新颁发后是否有任何证书属性保持不变?

X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);    
X509Certificate2Collection certificates = store.Certificates.Find(
                                                X509FindType.FindBySerialNumber,
                                                sslCertificateSerialNumber,
                                                false);

'sslCertificateSerialNumber' 来自配置文件

4

1 回答 1

0

您可以使用 findBySubjectName 它更有用。如果您有多个主题,您可以这样做:

        X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
        X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
        foreach (X509Certificate2 x509 in collection)
        {
            if (x509.Thumbprint == "5550541D10488D148BCAC0D289DED441609849FF")
            {
                client.ClientCredentials.ClientCertificate.SetCertificate(
                 x509.SubjectName.Name, store.Location, StoreName.TrustedPeople);
            }
        }
于 2020-12-17T07:06:06.273 回答