【问题标题】:CryptographicException: Access denied - How to give access on User store?CryptographicException:访问被拒绝 - 如何授予对用户存储的访问权限?
【发布时间】: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 帖子无法解决我的问题。我认为当前用户可能无法访问他自己的私钥存储。但是我怎样才能授予该访问权限?

【问题讨论】:

标签: c# wpf windows security


【解决方案1】:

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

【讨论】:

  • 这是我的问题。感谢您的解决方案!
  • 你能解释更多吗?我还不知道你是怎么解决这个问题的
【解决方案2】:

以防万一它对某人有帮助,“CryptographicException: Access denied”可能是由于磁盘空间不足引起的,我就是这样。

【讨论】:

    【解决方案3】:

    在我的情况下,这是由于缺少对 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 文件夹的访问权限。

    我的用户只有 Read 访问权限,一旦我授予 Write 访问权限,它就可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 2016-03-16
      • 2012-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多