我用 C# 编写了一个 ASP.NET Core 2.0 网站,并启用了 Facebook 身份验证,因此它需要 HTTPS。我使用本机 Kestrel Web 服务器来托管该网站,并设置一个侦听器来根据 MS 文档获取 PFX 证书。我似乎无法找到让 Kestrel 在从 Key Vault 调用后识别私钥的方法。我知道它存在,因为我写了两个调试语句表明它确实存在。
这是我用来检索秘密的函数,它正在工作。
public static async Task<X509Certificate2> GetKeyVaultCert()
{
X509Certificate2 pfx;
try
{
var kvClient = new KeyVaultClient(new KeyVaultClient.AuthenticationCallback(GetToken));
var secret = await kvClient
.GetSecretAsync("https://macscampvault.vault.azure.net/secrets/letsencrypt").ConfigureAwait(false);
byte[] bytes;
if(secret.ContentType == "application/x-pkcs12")
bytes = Convert.FromBase64String(secret.Value);
else
{
bytes = new byte[0];
Console.WriteLine("secret is not PFX!!");
throw new ArgumentException("This is not a PFX string!!");
}
var password = new SecureString();
var coll = new X509Certificate2Collection();
coll.Import(bytes, null, X509KeyStorageFlags.Exportable);
pfx = coll[0];
// File output added in case I end up needing to write cert to container
// File.WriteAllBytes(Directory.GetCurrentDirectory().ToString() + "/Macs.pfx", bytes);
Console.WriteLine(pfx.HasPrivateKey);
Console.WriteLine(pfx.GetRSAPrivateKey());
}
catch (Exception ex)
{
Console.WriteLine($"There was a problem during the key vault operation\n{ex.Message}");
throw;
}
return pfx;
}
赋值调用后的调试语句pfx = coll[0];
告诉我这个私钥存在,但是当我尝试使用连接到网站时lynx https://localhost
我收到以下异常:System.NotSupportedException: The server mode SSL must use a certificate with the associated private key.
那么,如何使用私钥呢?这是一个gist https://gist.github.com/digitaldelirium/b782938c9f15bf3ddbfef526d29a9f72到有问题的文件。
我已经得到了帮助如何在 Azure Key Vault 中序列化和反序列化 PFX 证书? https://stackoverflow.com/questions/33728213/how-to-serialize-and-deserialize-a-pfx-certificate-in-azure-key-vault?rq=1但跟着它之后,我就到了这个状态。
在你的要点中,你有以下代码:
var keyVaultCert = GetKeyVaultCert().Result ??
throw new ArgumentNullException("GetKeyVaultCert().Result");
pfx = new X509Certificate2(keyVaultCert.RawData);
第二行删除了私钥,因为RawData
属性仅返回 DER 编码的 X.509 对象。
keyVaultCert
已经是一个X509Certificate2
有了私钥,您可能只想使用它。
pfx = GetKeyVaultCert().Result ?? throw etc;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)