我正在尝试在WPF应用程序中从pfx文件加载证书,但它给我一个拒绝访问的错误。
System.Security.Cryptography.CryptographicException:拒绝访问。
在System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) 中 在System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) 中 在System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) 中 在System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password) 中 在HelloWorld.HelloClient.Models.Infrastructure.ReadCertificateFromPfxFile(String certificatePath, String password)中,最后一行引起了异常。如果以管理员身份运行,则能正常工作。问题似乎是X509Certificate2的默认构造函数试图将密钥存储在用户存储区域中。我没有使用Web应用程序。此post未解决我的问题。这篇帖子无法解决我的问题。我想当前用户可能没有访问自己私有密钥存储区域的权限。但我如何赋予该访问权限?
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 hr) 中 在System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) 中 在System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) 中 在System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password) 中 在HelloWorld.HelloClient.Models.Infrastructure.ReadCertificateFromPfxFile(String certificatePath, String password)中,最后一行引起了异常。如果以管理员身份运行,则能正常工作。问题似乎是X509Certificate2的默认构造函数试图将密钥存储在用户存储区域中。我没有使用Web应用程序。此post未解决我的问题。这篇帖子无法解决我的问题。我想当前用户可能没有访问自己私有密钥存储区域的权限。但我如何赋予该访问权限?