certcontext是一个无效的句柄

时间:2017-09-12 00:03:36

标签: c# certificate

我正在尝试将证书安装到本地计算机的受信任的根目录中。

这是我到目前为止所拥有的

  private void InstallCertificate()
    {
        X509Certificate2 certificate = new X509Certificate2();
        string CertFile = Environment.CurrentDirectory + "\\Resources\\cert.crt";
        X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadWrite);
        store.Add(certificate);
        store.Close();

我收到以下错误“certcontext是一个无效的句柄”,并想知道是否有人可以解释一下。

由于

1 个答案:

答案 0 :(得分:1)

    X509Certificate2 certificate = new X509Certificate2();
    string CertFile = Environment.CurrentDirectory + "\\Resources\\cert.crt";

有人假设您打算在某个时刻实际加载CertFile:

    string CertFile = Environment.CurrentDirectory + "\\Resources\\cert.crt";
    X509Certificate2 certificate = new X509Certificate2(CertFile);

目前,您有一个托管对象,它实际上代表了缺少证书。