2012-09-07 58 views
0

以下代碼在嘗試輸出PrivateKey或PublicKey時失敗並顯示以下消息。 (指紋將輸出正常。):正在讀取X.509證書2私鑰

該進程不具有此操作所需的'SeSecurityPrivilege'特權。

如果我以本地管理員身份運行,它將起作用。我如何解決這個問題。

fyi ..證書(pfx)受密碼保護 - 但不確定如何在此代碼段中指明該證書。

var certStore = new X509Store(StoreName.My, StoreLocation.LocalMachine); 
certStore.Open(OpenFlags.ReadOnly); 
string thumbprint = "D80FB0BB6485B6A2DE647812C5AA72A8F7ABA14C"; 

X509Certificate2Collection certCollection = certStore.Certificates.Find(
    X509FindType.FindByThumbprint, 
    thumbprint, false); 

// Close the certificate store. 
certStore.Close(); 

if (certCollection.Count == 0) 
{ 
    throw new SecurityException(string.Format(CultureInfo.InvariantCulture, "No certificate was found for thumbprint {0}", thumbprint)); 
} 
Console.WriteLine(certCollection[0].PrivateKey); 
+0

你從哪裏得到錯誤? – msmucker0527

+0

在Console.WriteLine中檢索PrivateKey或PublicKey屬性時。然而,我能夠輸出Thumbrprint。我在LINQPad中測試這段代碼,因爲這與我在自定義WCF ServiceHost中運行相同的邏輯(減去Console.WriteLine)時得到的錯誤消息相同。 –

回答

0

您需要授予該帳戶「管理審計和安全日誌權限」。有關更多信息,請參閱http://support.microsoft.com/kb/2000257/en-US。不過,這對證書操作來說很奇怪。

How to view permissions for RSA Key Container在這裏可能是相關的,因爲它討論了需要相同的特權來訪問私鑰。

該帳戶可能有權限,但可能需要啓用該權限。有關示例代碼,請參見C# Random Exception when Getting/Setting Registry ACL "SeSecurityPrivilege"