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,如下截图:
相关推荐
Mr_Tony7 小时前
Swift 中的 Combine 框架完整指南(含示例代码 + 实战)
开发语言·swift
无心水7 小时前
22、Java开发避坑指南:日期时间、Spring核心与接口设计的最佳实践
java·开发语言·后端·python·spring·java.time·java时间处理
Hello.Reader7 小时前
双卡 A100 + Ollama 最终落地手册一键部署脚本、配置文件、预热脚本与 Python 客户端完整打包
开发语言·网络·python
cch89187 小时前
汇编VS C++:底层控制与高效开发之争
java·开发语言
lifewange7 小时前
代码托管平台
开发语言
yangyanping201087 小时前
Go语言学习之配置管理库Viper
开发语言·学习·golang
橘子编程7 小时前
UniApp跨端开发终极指南
开发语言·vue.js·uni-app
冬至喵喵8 小时前
构建 CLI 的 Python 框架:Typer技术介绍
开发语言·chrome·python
AbandonForce8 小时前
STL list
开发语言·c++
前端老石人8 小时前
HTML 入门指南:从规范视角建立正确知识体系
开发语言·前端·html