阅读X.509证书2私钥

时间:2012-09-07 15:11:55

标签: c# .net ssl-certificate x509certificate

尝试输出PrivateKey或PublicKey时,以下代码失败并显示以下消息。 (指纹输出正常。):

  

该过程不具备此操作所需的“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);

1 个答案:

答案 0 :(得分:1)

您需要授予该帐户“管理审核和安全日志权限”。有关详细信息,请参阅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"

相关问题