Shi*_*uel 2 c# windows security wpf
我试图从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);
Run Code Online (Sandbox Code Playgroud)
System.Security.Cryptography.CryptographicException:拒绝访问.
在System.Security.Cryptography.CryptographicException.ThrowCryptographicException(的Int32小时)在System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(字节[] RAWDATA,IntPtr的密码,UInt32的dwFlags中,布尔persistKeySet,SafeCertContextHandle&pCertCtx)在System.Security.Cryptography位于HelloWorld.HelloClient.Models.Infrastructure.ReadCertificateFromPfxFile的System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte [] rawData,String password)的.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte [] rawData,Object password,X509KeyStorageFlags keyStorageFlags) (String certificatePath,String password)
代码段中的最后一行导致异常,如果我以管理员身份运行它就可以了.问题似乎是X509Certificate2的默认构造函数尝试将私钥放在用户存储中.我没有使用Web应用程序.这篇文章没有解决我的问题.我认为当前用户可能无法访问自己的私钥库.但是我怎么能提供这种访问权限?
din*_*dhi 11
在我的情况下,这是由于缺乏对C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys文件夹的写访问权限。
我的用户只有读取访问权限,一旦我授予写入访问权限,它就可以正常工作。
如果有人在寻找类似问题的解决方案,则发布修复程序.我运行了sysinternal进程监视器,并意识到构造函数调用正在机器密钥文件夹中创建一个密钥,并授予用户访问机器密钥的权限.
| 归档时间: |
|
| 查看次数: |
7356 次 |
| 最近记录: |