2012-09-07 59 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"