我在Azure网站(不是托管服务)中有网站,我需要在那里处理带有私钥的.pfx证书.
var x509Certificate2 = new X509Certificate2(certificate, password);
Run Code Online (Sandbox Code Playgroud)
但我遇到了以下异常:
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)
Run Code Online (Sandbox Code Playgroud)
在文章http://blog.tylerdoerksen.com/2013/08/23/pfx-certificate-files-and-windows-azure-websites/我发现它发生是因为默认情况下系统使用用户的本地目录存储密钥.但Azure网站没有本地用户配置文件目录.在同一篇文章中,作者建议使用X509KeyStorageFlags.MachineKeySetflag.
var x509Certificate2 = new X509Certificate2(certificate, password, X509KeyStorageFlags.MachineKeySet);
Run Code Online (Sandbox Code Playgroud)
但现在我还有其他例外:
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& …Run Code Online (Sandbox Code Playgroud) 我正在使用此类GoogleJsonWebToken生成一个访问令牌,用于对Google Calendar API的json调用.当我使用以下内容(使用我的实际服务帐户电子邮件)时,它在我的开发机器上的IIS Express中运行得非常好:
string p12Path = HttpContext.Current.Server.MapPath("~/App_Data/certificate.p12");
var auth = GoogleJsonWebToken.GetAccessToken("uniquestring@developer.gserviceaccount.com",
p12Path,
"https://www.googleapis.com/auth/calendar");
string Token = auth["access_token"];
Run Code Online (Sandbox Code Playgroud)
为了测试这个,我只是@Token在我的cshtml razor视图中调用.当我将其发布到我的Azure网站时,它不起作用.如果我不GoogleJsonWebToken修改我的课程,我会得到一个非常无益的502 - Web server received an invalid response while acting as a gateway or proxy server.,没有其他信息.
在一些谷歌搜索后,我发现这个SO帖子是一个类似的问题.所以System.Security.Cryptography.CryptographicException: The system cannot find the file specified.当我从Azure网站运行时,我尝试了他们的解决方案.当它从我的开发机器上运行,我得到System.Net.WebException: The remote server returned an error: (400) Bad Request.我认为这是因为该解决方案的CspKeyContainerInfo.KeyContainerName是null,而当我的dev的机器上运行未经修改的原始类的给我像{C0E26DC5-5D2C-4C77-8E40-79560F519588}每个时间是随机生成的这个值是在过程中使用签署签名.
然后我找到了这个SO帖子,但该解决方案产生的结果与上一个解决方案相同.
我也试过大多数不同的组合X509KeyStorageFlags …