如何以编程方式导入本地计算机密钥库中的私钥?

发布于 2025-01-02 02:32:26 字数 1285 浏览 0 评论 0原文

如何以编程方式导入本地计算机密钥库中的私钥?

实际上我正在测试这段代码:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2025-01-09 02:32:26

MSDN 有一个解决方案。只需添加一个标志 X509KeyStorageFlags.MachineKeySet 即可正常运行:

cert = new X509Certificate2(certificatePath,
                            certificatePassword,
                            X509KeyStorageFlags.MachineKeySet);

The MSDN has a solution. Simply add a flag X509KeyStorageFlags.MachineKeySet and it runs fine:

cert = new X509Certificate2(certificatePath,
                            certificatePassword,
                            X509KeyStorageFlags.MachineKeySet);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文