CryptographicException未处理:系统找不到指定的文件

Sto*_*orm 56 .net c# cryptographicexception x509certificate

我试图接受SSL通信的奥秘,并在这个网站上找到了一个很棒的教程.我试图测试我自己的证书.使用Visual Studio 2012,我只是添加了一个现有文件(我的证书为.pfx格式),然后更改了app.config中的"certificate"和"password"设置.但是,当试图运行它时,我收到一个错误:

CryptographicException未处理:系统找不到指定的文件

然后,我在我的Web服务中尝试了相同的操作.我在那里得到了关于错误的更多细节:

System.Security.Cryptography.CryptographicException: System cannot find specified file.

   at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
   v System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)
   v TestServer.DataService.LoadSoap() v c:\Users\Administrator\Documents\Visual Studio 2012\Projects\TestServer\TestServer\DataService.asmx.cs:line 48
Run Code Online (Sandbox Code Playgroud)

我已经把这个问题写给了文章的作者,但自从他上次回复是在2012年3月,我不确定,他是否会回复.如果有人可以帮我解决这个问题,我将非常感激.

PS:将证书从.cer导出到.pfx时,我已经更改了导出文件的标题.虽然我怀疑它对这个问题的影响,但我宁愿提一下.

nol*_*ogo 191

您是否在IIS中的应用程序池中设置了以下内容?

  1. 转到IIS管理器
  2. 转到应用程序池实例
  3. 单击高级设置
  4. 在Process model下,将Load User Profile设置为true

请参阅此堆栈问题以进一步阅读:当我设置IIS池的LoadUserProfile时会发生什么?

  • 不用担心 - 对于偶然发现这种情况的其他人来说更是如此.我有同样的错误,并错过了我写的iis设置:) (12认同)
  • 你很棒!感谢您考虑"对于那些偶然发现这种情况的人来说更是如此." 这正是我所需要的. (9认同)
  • 有效.这太荒谬了!到底是怎么回事? (3认同)
  • 同上。这是同样有价值的答案。虽然它不能解决提问者的问题,但它是解决此错误的另一种方法。 (2认同)
  • 我的天啊。多么荒谬的错误!!我尝试检查并检查,但没有发现文件名有任何问题。***t 加密异常告诉错误的文件路径,而实际上它不是 (2认同)
  • 在 Azure 中:使用应用程序设置 -> 添加值为“1”的“WEBSITE_LOAD_USER_PROFILE”。 (2认同)

Sal*_*ias 8

对于那些尝试使用Import方法导入X509Certificate2时收到Cryptographic Exception的人,我发现对MachineKeySet使用Enum选项绕过了在IIS中创建userContext的需要,因此更易于实现。

X509Certificate2 cert = new X509Certificate2();
cert.Import(certificateFilePath, certPasshrase, 
X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
Run Code Online (Sandbox Code Playgroud)

  • 只要这样做就可以快乐。在 Windows 10 2004 中使用 IIS 进行电臀舞对我来说不起作用。我尝试了所有可能的组合。这就像一个魅力 (2认同)

m12*_*rpv 6

因为这个问题具有很高的搜索排名,所以我想提出一种方法来向X509Certificate2提供一个绝对路径(它只接受)到ASP.net应用程序中相对定位的pxf密钥文件.

    string path = HttpContext.Current.Server.MapPath("~") + "..\keys\relative_key.pfx";

    X509Certificate2 cert = new X509Certificate2(path, "", X509KeyStorageFlags.DefaultKeySet);
Run Code Online (Sandbox Code Playgroud)


小智 6

通过传递带有标志csdMachineKeyKeyStore的CspParameters,IIS可以绕过抛出异常的限制.

CspParameters cspParams = new CspParameters();
cspParams.KeyContainerName = Guid.NewGuid().ToString().ToUpperInvariant();
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(cspParams);
Run Code Online (Sandbox Code Playgroud)

我在这里找到了解决方

链接到信息资源


小智 6

您需要指定绝对路径而不是相对路径。

AppDomain.CurrentDomain.BaseDirectory +"/Certificate/cer.PFX"
Run Code Online (Sandbox Code Playgroud)


小智 5

对于尝试从 Windows 证书存储(证书(本地计算机))访问证书的用户,请确保使用操作 -> 所有任务 -> 管理私钥...为应用程序池用户授予对证书私钥的访问权限。事实证明,不这样做是我收到此错误的原因。