Azure网站中的站点无法处理X509Certificate2

我在Azure网站(不是托管服务)中有网站,我需要在那里处理带有私钥的.pfx证书。

var x509Certificate2 = new X509Certificate2(certificate, password); 

但我面临以下例外:

 System.Security.Cryptography.CryptographicException: The system cannot find the file specified. at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) 

在文章http://blog.tylerdoerksen.com/2013/08/23/pfx-certificate-files-and-windows-azure-websites/我发现它发生是因为默认情况下系统使用用户的本地目录存储密钥。 但Azure网站没有本地用户配置文件目录。 在同一篇文章中,作者建议使用X509KeyStorageFlags.MachineKeySet标志。

 var x509Certificate2 = new X509Certificate2(certificate, password, X509KeyStorageFlags.MachineKeySet); 

但现在我还有其他例外:

 System.Security.Cryptography.CryptographicException: Access denied. at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx) at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags) 

任何人都可以帮助我理解它为什么会发生以及如何解决它?

我猜你找到了一个解决方法,但如果其他人正在努力解决这个问题,我在另一个SO问题中找到了答案:

如何从PKCS#12字节数组构造X509Certificate2抛出CryptographicException(“系统找不到指定的文件。”)?

神奇的是指定X509KeyStorageFlags存储标志。 例:

 var myCertificae = new X509Certificate2( certificateData, securePasswordString, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 

Azure网站现在支持将证书安装到证书库。 你有没试过?

详情请访问: http : //azure.microsoft.com/blog/2014/10/27/using-certificates-in-azure-websites-applications/

Azure网站在共享环境中运行。 我假设证书的构造函数试图在实例上创建一些临时信息,但它没有权限。

您可能必须升级到托管服务才能在提升的上下文中运行并执行此工作。

另外,您validation密码是否正确? 如果它不需要密码,则至少必须将string.Empty传递给构造函数。 传入NULL值也会导致此exception。

我有完全相同的问题,并花了很多时间修复它。 在你提到最后一次堆栈调用的文章中,函数是LoadCertFromFile ,但在你(和我的)情况下,它是LoadCertFromBlob

所以我找了LoadCertFromBlob并发现了这个:

为什么X509Certificate2有时无法从blob创建?

解决方案是进入IIS并将应用程序池标识从“ApplicationPoolIdentity”更改为“LocalService”,以便将证书加载到正确的本地文件夹中。

在Azure网站/ Web应用程序/移动应用程序中 – 您必须使用全部用于导入SSL证书的应用程序服务计划 – 因此它不应该是免费或共享。 您不仅可以导入SSL证书,还可以导入示例代码签名证书,并在signtool或PowerShell中使用它。

我在https://vmplace.eu/中使用了这种方法

如果您尝试使用免费或共享计划,则会收到错误 – 因此在Azure中,这些计划中还有其他版本的.NET框架。

您也可以参考这个项目: https : //github.com/onovotny/SignService

mvpbuzz