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,如下截图:
相关推荐
朱永博4 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
前端H4 小时前
Biome & Rolldown:Rust 工具链接管前端基建
开发语言·前端·rust
天天进步20154 小时前
UI-TARS 源码解析 #4:UI-TARS 与传统 RPA 的区别:为什么它不是简单的坐标点击脚本?
开发语言
运维行者_4 小时前
广域网性能监控:分布式IT架构下的链路质量保障
开发语言·网络·分布式·后端·架构·数据库架构
梅雅达编程笔记4 小时前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas
灯澜忆梦4 小时前
GO---map函数
开发语言·前端·后端·golang
北冥you鱼5 小时前
Go语言与默克尔树:区块链数据完整性的基石
开发语言·golang·区块链
库克克5 小时前
【C++】类和对象--this指针详解
java·开发语言·c++
JackieZhengChina5 小时前
image-viewer-js 开源图片查看插件完整教程
开发语言·javascript·开源
光影少年5 小时前
react的View/Text/Image/ScrollView 常用组件注意事项
开发语言·前端·javascript·react.js·前端框架