x509Certificate2返回null-SSL证书

时间:2019-06-17 14:58:40

标签: .net-core azure-service-fabric

我已经安装了ssl证书,但是当我使用certCollection.Find by Extension时,它返回的是null。

private X509Certificate2 GetCertificateFromStore()
    {

        X509Certificate2 x509Certificate2;

        var aspNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        if (string.Equals(aspNetCoreEnvironment, "Development", StringComparison.OrdinalIgnoreCase))
        {
            const string aspNetHttpsOid = "1.3.1.1.3.1.1.84.1.1";
            const string cnName = "CN=localhost";
            using (var store = new X509Store(StoreName.My, StoreLocation.LocalMachine))
            {
                store.Open(OpenFlags.ReadOnly);
                var certCollection = store.Certificates;
                var currentCerts = certCollection.Find(X509FindType.FindByExtension, aspNetHttpsOid, true);
                currentCerts = currentCerts.Find(X509FindType.FindByIssuerDistinguishedName, cnName, true);
                x509Certificate2 = currentCerts.Count == 0 ? null : currentCerts[0];
            }
        }
    }

注意:我已经使用以下命令创建了自签名开发证书。

PS C:\program files\microsoft sdks\service fabric\clustersetup\secure> .\CertSetup.ps1 -Install -CertSubjectName CN=mytestcert

安装后,我可以看到证书已创建并添加到“受信任的根”中。

enter image description here

在创建自签名证书时我犯了一些错误。我不确定。

1 个答案:

答案 0 :(得分:1)

您已将证书存储在StoreLocation.CurrentUser中,但是代码正在StoreLocation.LocalMachine中查找。

此外,请不要忘记添加适当的ACL's