Tôi đã có một giải pháp cho vấn đề này và nghĩ rằng sẽ đăng nó ở đây để có thể giúp những người khác đang gặp phải vấn đề tương tự.
Nó không hoạt động với các tệp .pem. Tôi đã chuyển đổi nó thành tệp .pfx bằng lệnh dưới đây và nó bắt đầu hoạt động tốt.
openssl pkcs12 -inkey C:\Certs\client-key.pem -in C:\Certs\client-cert.pem -export -out C:\Certs\client-cert.pfx
Tham khảo: Hỗ trợ xác thực chứng chỉ
CHỈNH SỬA
Thay vì tạo tệp pfx vật lý, tôi đã có thể kết hợp hai tệp pem và làm cho nó hoạt động. Đoạn mã được cung cấp bên dưới để ai đó tham khảo trong tương lai.
public X509Certificate2 GetCombinedCertificateAndKey(string certificatePath, string privateKeyPath)
{
using var publicKey = new X509Certificate2(certificatePath);
var privateKeyText = System.IO.File.ReadAllText(privateKeyPath);
var privateKeyBlocks = privateKeyText.Split("-", StringSplitOptions.RemoveEmptyEntries);
var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);
using var rsa = RSA.Create();
if (privateKeyBlocks[0] == "BEGIN PRIVATE KEY")
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
}
else if (privateKeyBlocks[0] == "BEGIN RSA PRIVATE KEY")
{
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
}
var keyPair = publicKey.CopyWithPrivateKey(rsa);
var Certificate = new X509Certificate2(keyPair.Export(X509ContentType.Pfx));
return Certificate;
}