.net core x509Certificate2返回null-ssl证书
我已经安装了ssl证书,但是当我使用certCollection.Find by Extension时,它返回null.net core x509Certificate2返回null-ssl证书,.net-core,azure-service-fabric,.net Core,Azure Service Fabric,我已经安装了ssl证书,但是当我使用certCollection.Find by Extension时,它返回null private X509Certificate2 GetCertificateFromStore() { X509Certificate2 x509Certificate2; var aspNetCoreEnvironment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIR
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
安装后,我可以看到证书已创建并添加到受信任的根目录中
我是否在创建自签名证书时出错。我不确定。您已将证书存储在
StoreLocation.CurrentUser
中,但您的代码正在StoreLocation.LocalMachine
中查找
另外,别忘了添加正确的选项