如何以编程方式导入本地计算机密钥库中的私钥?
如何以编程方式导入本地计算机密钥库中的私钥?
实际上我正在测试这段代码:
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 中。我应该怎么办?
How to import programmally a private key in the local machine keystore?
Actually I'm testing this code:
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);
}
}
All tries to get the NetworkService token failed yet. The code here doesn't work with admin privileges.
The code above imports the private key to the currient user instat of the local machine. What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MSDN 有一个解决方案。只需添加一个标志
X509KeyStorageFlags.MachineKeySet
即可正常运行:The MSDN has a solution. Simply add a flag
X509KeyStorageFlags.MachineKeySet
and it runs fine: