2012-02-02 11 views
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のトークンがまだ失敗を取得しようとします。コードhereは管理者権限では機能しません。

上記のコードは、ローカルマシンのcurrient user instatに秘密鍵をインポートします。私は何をすべきか?

答えて

1

MSDNにはsolutionがあります。フラグX509KeyStorageFlags.MachineKeySetを追加するだけで、正常に動作します。

cert = new X509Certificate2(certificatePath, 
          certificatePassword, 
          X509KeyStorageFlags.MachineKeySet); 
関連する問題