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,如下截图:
相关推荐
Cao12345678932117 分钟前
简易学生成绩管理系统(C语言)
c语言·开发语言
The Future is mine18 分钟前
C# new Bitmap(32043, 32043, PixelFormat.Format32bppArgb)报错:参数无效,如何将图像分块化处理?
开发语言·c#
亿坊电商21 分钟前
PHP框架在微服务迁移中能发挥什么作用?
开发语言·微服务·php
烁34721 分钟前
每日一题(小白)模拟娱乐篇33
java·开发语言·算法
坐吃山猪40 分钟前
Python-Agent调用多个Server-FastAPI版本
开发语言·python·fastapi
88号技师42 分钟前
【1区SCI】Fusion entropy融合熵,多尺度,复合多尺度、时移多尺度、层次 + 故障识别、诊断-matlab代码
开发语言·机器学习·matlab·时序分析·故障诊断·信息熵·特征提取
北漂老男孩1 小时前
Java对象转换的多种实现方式
java·开发语言
未来可期LJ1 小时前
【Test】单例模式❗
开发语言·c++
Arenaschi1 小时前
SQLite 是什么?
开发语言·网络·python·网络协议·tcp/ip
听雨·眠1 小时前
go语言中defer使用指南
开发语言·后端·golang