C# 加密异常:访问被拒绝-如何授予对用户存储的访问权限?

C# 加密异常:访问被拒绝-如何授予对用户存储的访问权限?,c#,wpf,windows,security,C#,Wpf,Windows,Security,我试图从WPF应用程序中的pfx文件加载证书,但它给了我一个拒绝访问错误 using (FileStream stream = System.IO.File.OpenRead(certificatePath)) { using (BinaryReader reader = new BinaryReader(stream)) { buffer = reader.ReadBytes((int)stream.Length); } } X509Certifica

我试图从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.Cryptography异常:访问被拒绝。
在 系统。安全。加密。加密异常。ThrowCryptographicException(Int32 hr)在 System.Security.Cryptography.X509Certificates.X509Utils.\u LoadCertFromBlob(字节[]) rawData、IntPtr密码、UInt32 dwFlags、布尔键集、, SafeCertContextHandle和pCertCtx)位于 System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(字节[]) rawData,对象密码,X509keystrageFlags keystrageFlags)位于 System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(字节[]) rawData,字符串密码)位于 HelloWorld.HelloClient.Models.Infrastructure.ReadCertificateFromPfxFile(字符串 证书路径,字符串密码)


代码段中的最后一行导致了异常,如果我以管理员身份运行它,它可以正常工作。问题似乎是X509Certificate2的默认构造函数试图将私钥放入用户存储中。我没有使用web应用程序。这篇文章不能解决我的问题。我认为当前用户可能无法访问自己的私钥存储。但是我怎么才能给你这个机会呢

如果有人正在寻找类似问题的解决方案,则发布修复。我运行并意识到构造函数调用正在计算机密钥文件夹中创建密钥,并授予用户对计算机密钥的写入权限。

为了防止对某人有所帮助,光盘中的空间不足可能会导致“加密异常:拒绝访问”,这就是我的情况。

可能与@MethodMan重复,它在谈论IIS中的web应用程序和修复程序。我有WPF应用程序我如何才能授予用户存储权限?这是我的问题。谢谢你的解决方案!你能再解释一下吗?我还不明白你是怎么解决这个问题的