如何在本地机器密钥库中以编程方式导入私钥
本文关键字:编程 方式 私钥 导入 密钥 机器 | 更新日期: 2023-09-27 18:22:08
如何在本地机器密钥库中以编程方式导入私钥?
实际上我正在测试这个代码:
private static void InstallCertificate(string certificatePath, string certificatePassword, StoreName store) {
try {
var serviceRuntimeUserCertificateStore = new X509Store(store, StoreLocation.LocalMachine);
serviceRuntimeUserCertificateStore.Open(OpenFlags.ReadWrite);
X509Certificate2 cert;
try {
cert = new X509Certificate2(certificatePath, certificatePassword);
} catch(Exception ex) {
Console.WriteLine("Failed to load certificate " + certificatePath);
throw new DataException("Certificate appeared to load successfully but also seems to be null.", ex);
}
serviceRuntimeUserCertificateStore.Add(cert);
serviceRuntimeUserCertificateStore.Close();
} catch(Exception) {
Console.WriteLine("Failed to install {0}. Check the certificate index entry and verify the certificate file exists.", certificatePath);
}
}
所有获取NetworkService令牌的尝试都失败了。这里的代码不适用于管理员权限。
上面的代码将私钥导入到本地机器的当前用户instat。我该怎么办?
X509KeyStorageFlags.MachineKeySet
,它就会运行良好:
cert = new X509Certificate2(certificatePath,
certificatePassword,
X509KeyStorageFlags.MachineKeySet);