证书是这样生成的:
1. 生成密钥对
keytool -genkeypair -dname "cn=clientAuth_testing, ou=IS, o=SGM, c=CN" -alias clientAuthCert -keypass Pass1234 -keystore d:\clientAuth_testing.jks -storepass Pass1234 -validity 3600 -keyalg RSA -keysize 2048 -sigalg SHA256WithRSA
2. 导出公钥发给SGM
keytool -export -file d:\clientAuth_testing.cer -keystore d:\clientAuth_testing.jks -storepass Pass1234 -alias clientAuthCert
3. p12格式证书
keytool -importkeystore -srckeystore d:\clientAuth_testing.jks -srcstoretype JKS -deststoretype PKCS12 -destkeystore d:\clientAuth_testing.p12
我这边用的是.p12证书,在我Windows 11电脑,代码C#写的,运行没有问题
X509Certificate2 cert = null;
try
{
log.Info($"InstallCertificate_cert=>certFilePpasswordath:{password}");
log.Info($"InstallCertificate_cert=>certFilePath:{certFilePath}");
if (!File.Exists(certFilePath))
{
return cert;
}
byte[] certData = File.ReadAllBytes(certFilePath);
cert = new X509Certificate2(certData, password, X509KeyStorageFlags.DefaultKeySet);
X509Store store = new X509Store(storeName, location);
store.Open(OpenFlags.MaxAllowed);
store.Remove(cert);
store.Add(cert);
store.Close();
return cert;
}
catch (Exception ex)
{
log.Error($"InstallCertificate_cert:=>" + ex.Message);
throw new Exception(ex.Message);
}
而且直接点击安装p12的证书也没有密码问题
不过在Windows Server 2012 R2,Windows Server 2016上都是提示:"指定的网络密码不正确",C#代码也一样的错误
就不知道怎么办了。
第一次使用这个东西,不知道哪个地方出错了,请指导
(Windows Server 2012 R2上Apifox中settings 加入证书没有问题)