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中的应用程序池中设置了以下内容?
请参阅此堆栈问题以进一步阅读:当我设置IIS池的LoadUserProfile时会发生什么?
对于那些尝试使用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)
因为这个问题具有很高的搜索排名,所以我想提出一种方法来向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 证书存储(证书(本地计算机))访问证书的用户,请确保使用操作 -> 所有任务 -> 管理私钥...为应用程序池用户授予对证书私钥的访问权限。事实证明,不这样做是我收到此错误的原因。