【发布时间】:2016-10-26 04:46:06
【问题描述】:
我正在尝试从 WPF 应用程序中的 pfx 文件加载证书,它给了我访问被拒绝的错误。
using (FileStream stream = System.IO.File.OpenRead(certificatePath))
{
using (BinaryReader reader = new BinaryReader(stream))
{
buffer = reader.ReadBytes((int)stream.Length);
}
}
X509Certificate2 certificate = new X509Certificate2(buffer, password);
System.Security.Cryptography.CryptographicException:访问被拒绝。
在 System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 小时)在 System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(字节[] rawData, IntPtr 密码, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle & pCertCtx) 在 System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(字节[] rawData,对象密码,X509KeyStorageFlags keyStorageFlags)在 System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(字节 [] rawData,字符串密码)在 HelloWorld.HelloClient.Models.Infrastructure.ReadCertificateFromPfxFile(字符串 证书路径,字符串密码)
sn-p 中的最后一行导致异常,如果我以管理员身份运行它,它工作正常。问题似乎是 X509Certificate2 的默认构造函数试图将私钥放在用户存储中。我没有使用网络应用程序。 this 帖子无法解决我的问题。我认为当前用户可能无法访问他自己的私钥存储。但是我怎样才能授予该访问权限?
【问题讨论】:
-
@MethodMan 它谈论Web应用程序和IIS中的修复。我有 WPF 应用程序如何授予用户存储权限?