2014-06-23 16 views
3

我有一個在端口9000上運行本地主機的.NET 4.0程序。 我想要支持SSL並有一個.pfx證書來導入。c#X509Certificate2添加證書如何標記爲可導出

由於程序在多臺計算機上運行,​​程序本身負責 存儲證書並註冊其端口。

當程序導入並註冊時,一切正常。 但是,當我重新啓動計算機的HTTPS連接不起作用任何..更多..

事件查看器提供了以下錯誤:

A fatal error occurred when attempting to access the SSL server credential private key. The error code returned from the cryptographic module is 0x8009030D. The internal error state is 10001.

An error occurred while using SSL configuration for endpoint 0.0.0.0:9000. The error status code is contained within the returned data.

我找到了一些解決方案其中聲明您需要在導入 時將該證書標記爲「可導出」,但我無法找到代碼中如何執行該操作。

Website 1Website 2

存放證書:

String path = String.Concat(IOHelper.AssemblyPath, @"\certificate.pfx"); 
X509Certificate2 cert = new X509Certificate2(path, "password"); 

X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
store.Open(OpenFlags.ReadWrite); 

if(!store.Certificates.Contains(cert)) 
{ 
    store.Add(cert); 
} 

註冊主機:

netsh http add sslcert ipport=0.0.0.0:9000 certhash=<cert hash> appid=<app id> 

回答

2

爲了紀念一個X509Certificate2爲可導出其實很簡單。您添加第三個參數(X509KeyStorageFlags.Exportable),它指示證書是可導出的。

X509Certificate2 certificate = new X509Certificate2(path, "password", X509KeyStorageFlags.Exportable);