C# 使用 RSA 加密算法生成证书签名产生“The system cannot find the file specified”异常

使用 C# 中 RSA(System.Security.Cryptography.RSA) 加密算法生成证书签名进行身份验证,在 VS2022 开发工具本地运行应用程序一切正常。

但将应用程序部署到远程服务器(如:Azure App Services),生成签名时产生如下错误信息:

cs 复制代码
System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
   at System.Security.Cryptography.CngKey.Import(ReadOnlySpan`1 keyBlob, String curveName, CngKeyBlobFormat format, CngProvider provider)
   at System.Security.Cryptography.CngPkcs8.ImportPkcs8(ReadOnlySpan`1 keyBlob)
   at System.Security.Cryptography.CngPkcs8.ImportPkcs8PrivateKey(ReadOnlySpan`1 source, Int32& bytesRead)
   at System.Security.Cryptography.RSACng.ImportPkcs8PrivateKey(ReadOnlySpan`1 source, Int32& bytesRead)

这很可能是因为 Cryptographic Service 提供程序试图在用户存储区中存储或加载证书的密钥,并且由于配置文件不可用,因此加密上下文不可用。

由于应用程序服务(Azure App Serivces)默认没有开启加载用户配置文件设定。

解决上述问题:

可在Azure应用程序服务 >> 配置 >> Application settings,增加 WEBSITE_LOAD_USER_PROFILE = 1 的应用配置信息即可。

若应用程序部署在 Windows IIS 服务器上,则可以在应用程序池开启"加载用户配置文件"设定即可,设定步骤如下:

  1. 打开IIS管理器。
  2. 进入应用程序池管理页面,点选中要设定的应用程序池。
  3. 在右边"操作"模块下点击"高级设置..."或鼠标右键选择"高级设置..."。
  4. 在"进程模型(Process Model)"节点,将"加载用户配置文件(Load User Profile) "设为 True,如下截图:
相关推荐
freshman_y19 小时前
经典的C语言题型
c语言·开发语言·算法
small_wh1te_coder19 小时前
拷打字节技术总监: 详解c语言嵌入式多线程编程中的头文件 #总结 上下篇合 #
c语言·开发语言·算法·操作系统·嵌入式
凌盛羽19 小时前
使用python绘图分析电池充电曲线
开发语言·python·stm32·单片机·fpga开发·51单片机
wenroudelang888819 小时前
Visual Studio的C#实例--2个窗体之间跳转
ide·c#·visual studio
wangjialelele19 小时前
现代C++:C++17新特性整理
c语言·开发语言·c++·visual studio code
肖恭伟19 小时前
Curso调试Qt:GDB + Qt 官方 qt5printers.py + .gdbinit
开发语言·qt
思茂信息19 小时前
CST软件加载 Pin 二极管的可重构电桥仿真研究
服务器·开发语言·人工智能·php·cst·电磁仿真·电磁辐射
荔枝吻19 小时前
【AI总结】C#与.NET:一段跨越20年的命名纠葛与共生传奇
开发语言·c#·.net
xieliyu.19 小时前
Java、多态
java·开发语言
天天学IT19 小时前
第二章 Qt 模块
开发语言·qt·qt教程·qt6教程