【Azure 应用服务】在App Service中调用外部服务API时需要携带客户端证书,而多次调用的情况下会出现WindowsCryptographicException Keyset does not exist异常
问题描述
在App Service中调用外部服务API时需要携带客户端证书,而多次调用的情况下会出现WindowsCryptographicException Keyset does not exist异常。
错误Tarce:
Exception:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: Keyset does not exist
at Internal.NativeCrypto.CapiHelper.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeProvHandle()
at System.Security.Cryptography.RSACryptoServiceProvider.get_SafeKeyHandle()
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 keySize, CspParameters parameters, Boolean useDefaultKeySize)
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(CspParameters parameters)
at Internal.Cryptography.Pal.CertificatePal.<>c.<GetRSAPrivateKey>b__66_0(CspParameters csp)
at Internal.Cryptography.Pal.CertificatePal.GetPrivateKey[T](Func`2 createCsp, Func`2 createCng)
at Internal.Cryptography.Pal.CertificatePal.GetRSAPrivateKey()
at Internal.Cryptography.Pal.CertificateExtensionsCommon.GetPrivateKey[T](X509Certificate2 certificate, Predicate`1 matchesConstraints)
at xxxxxxxx.Paylink.WeChatPay.WeChatPayOptions.GetCertificateInfo()
问题分析
当在App Service中上传证书后,可以通过Kudu站点来查看证书是否存在。在Currentuser\My目录下则使用以下PowerShell指令:
Get-ChildItem Cert:\currentuser\My\
通过PowerShell命令查看证书是否存在,并没有能解决App Service中的 “WindowsCryptographicException Keyset does not exist” 问题,只是说从侧面证明当前实例中是否有证书。如果没有,那么错误非常明显,就需要在实例中安装证书。
但是如果证书还是存在,而错误情况依旧,通过多次验证发现于启用了App Service多实例有关。 虽然没有证明为什么在多实例的情况下出现证书获取不到的情况,但是当使用App Service单实例后,问题不在发生。
个人猜想:是否时在代码中加载证书时候的代码在多实例的情况下,某一个实例出现没有加载到证书的情况而导致。
PS: 记录App Service上出现的证书问题,以待未来再次遇见而参考。有新的发现在更新博文。
For more details, you could refer to the links below.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-03-23 【Azure 事件中心】Event Hub Client 连接超时(OperationTimeout)测试及解说