C# Azure密钥值和过期证书

C# Azure密钥值和过期证书,c#,azure,certificate,azure-keyvault,C#,Azure,Certificate,Azure Keyvault,我希望我能正确地解释这一点。我继承了两个windows应用程序,它们需要在本地证书存储中安装一个证书,以便访问Azure密钥库的机密来执行应用程序的操作。目前一切正常。Azure中的证书将于2019年10月31日到期 已创建一个新证书,该证书将于2020年9月到期 当我把这些应用程序扔给我时,我得到了证书可以使用,但它有一个.p12扩展名。我只能将新Azure证书导出为.cer或.pfx 当我将新导出的证书安装为.pfx或.cer时,应用程序将失败。如果我安装了扩展名为.p12的旧证书,它们就可

我希望我能正确地解释这一点。我继承了两个windows应用程序,它们需要在本地证书存储中安装一个证书,以便访问Azure密钥库的机密来执行应用程序的操作。目前一切正常。Azure中的证书将于2019年10月31日到期

已创建一个新证书,该证书将于2020年9月到期

当我把这些应用程序扔给我时,我得到了证书可以使用,但它有一个.p12扩展名。我只能将新Azure证书导出为.cer或.pfx

当我将新导出的证书安装为.pfx或.cer时,应用程序将失败。如果我安装了扩展名为.p12的旧证书,它们就可以工作了

这两个应用程序都使用下面的代码来获取(我认为)本地证书,该证书是通过“发卡机构”当前的,即CN=Value。我检查了“Issuer/CN=”的新旧值,它们是相同的

Azure中导出的证书是否需要.p12扩展名?如果是这样,我该怎么做

如果Azure中导出的证书可以作为.pfx使用,我的问题可能在哪里

获得本地证书的应用程序中的C#代码反过来又获得必要的Azure机密来完成工作:

private static X509Certificate2 ReadCertificateFromStore(string certName)
    {
        X509Certificate2 cert = null;

        try
        {
            using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = store.Certificates;

                // Find unexpired certificates.
                X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);

                // From the collection of unexpired certificates, find the ones with the correct name.
                X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindBySubjectDistinguishedName, certName, false);

                // Return the first certificate in the collection, has the right name and is current.
                cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return cert;
    }
私有静态X509Certificate2 ReadCertificateFromStore(字符串certName)
{
X509Certificate2Cert=null;
尝试
{
使用(X509Store store=新的X509Store(StoreName.My,StoreLocation.CurrentUser))
{
打开(OpenFlags.ReadOnly);
X509Certificate2Collection certCollection=store.Certificates;
//查找未过期的证书。
X509Certificate2Collection currentCerts=certCollection.Find(X509FindType.FindByTimeValid,DateTime.Now,false);
//从未过期证书集合中,找到名称正确的证书。
X509Certificate2Collection signingCert=currentCerts.Find(X509FindType.FindBySubjectDifferentizedName,certName,false);
//返回集合中的第一个证书,该证书具有正确的名称且为当前证书。
cert=signingCert.OfType().OrderByDescending(c=>c.NotBefore).FirstOrDefault();
}
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
返回证书;
}

首先
.p12
以及
.pfx
是PKCS#12格式的扩展

这两个应用程序都使用下面的代码来获取(我认为)本地证书,该证书是通过“发卡机构”当前的,即CN=Value。我检查了“Issuer/CN=”的新旧值,它们是相同的

基于不正确的代码

//从未过期的证书集合中,找到名称正确的证书。
X509证书2集合签名证书=
currentCerts.Find(X509FindType.FindBySubjectDifferentizedName,certName,false);
它表示
findbysubjectdiscriminatedname
,这意味着两个证书的
主题必须完全相同。以下是一个例子:

另一个主题包含多个元素:

您还可以同时安装这两个证书,并尝试找出参数以获得正确的证书。我已将部分代码转换为PowerShell:

$store = 
  new-object System.Security.Cryptography.X509Certificates.X509Store( `
    [System.Security.Cryptography.X509Certificates.StoreName]::My, `
    [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser);

$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly);

$signingCert = 
   $store.Certificates.Find(
     [System.Security.Cryptography.X509Certificates.X509FindType]::FindBySubjectDistinguishedName, 
     "CN=...", `
     $false);

$signingCert

谜团解开了。除了在有问题的机器上安装证书外,您还需要在Azure的应用程序注册中注册证书(.cer部分)。

这里有一些附加信息:我认为证书的.p12与.pfx并不重要。我从Azure导出了旧证书的.pfx版本,它运行良好。我一次又一次地查看了主题“CN=abc 123”信息。它们是相同的。这才是我真正困惑的地方。我甚至将它们复制/粘贴到一个固定的字体编辑器中,一个在另一个之上。它们是完全相同的。两行中只有一行。