尝试输出 PrivateKey 或 PublicKey 时,以下代码失败并显示以下消息。(指纹将输出正常。):
该进程不具备此操作所需的“SeSecurityPrivilege”特权。
如果我以本地管理员身份运行,它可以工作。我该如何解决这个问题。
仅供参考.. 证书 (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);