我们正在Kubernetes上开发微服务应用程序。 其中一个微服务是IdentityServer实例。 最初,我想在Docker上本地测试解决方案,以确保它能正常运行。 为此,我想将证书复制到appsettings.json中。 最终,此值将由Kubernetes secret替换。 在我的启动类中,这是我尝试加载证书的方式:
services.AddIdentityServer()
.AddSigningCredential(GetIdentityServerCertificate())
.AddConfigurationStore(...
private X509Certificate2 GetIdentityServerCertificate()
{
var clientSecret = Configuration["Certificate"];
var pfxBytes = Convert.FromBase64String(clientSecret);
var certificate = new X509Certificate2(pfxBytes, "PasswordHere");
return certificate;
}
证书是由我使用 OpenSSL 生成的:
openssl req –newkey rsa:2048 –nodes –keyout XXXXX.key –x509 –days 365 –out XXXXX.cer
openssl pkcs12 –export –in XXXX.cer –inkey XXXX.key –out XXXX.pfx
然后我使用以下代码获取证书:
$pfxFilePath = 'C:\XXXX.pfx'
$pwd = 'PasswordHere'
$flag = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$collection = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$collection.Import($pfxFilePath, $pwd, $flag)
$pkcs12ContentType = [System.Security.Cryptography.X509Certificates.X509ContentType]::Pkcs12
$clearBytes = $collection.Export($pkcs12ContentType)
$fileContentEncoded = [System.Convert]::ToBase64String($clearBytes)
我获取$fileContentEncoded
的值并将其粘贴到appsettings.json
中。
当我进行调试时,尝试使用上述方法创建X509Certificate2
对象时,结果为:error:23076071:PKCS12 routines:PKCS12_parse:mac verify failure
。