c# - 从asp.net核心资源中读取嵌入文件
问题描述
将我的网站发布到 iis 后,出现以下错误:
Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException:系统找不到指定的文件
我从嵌入式文件中读取 X509Certificate2 的代码是:
X509Certificate2 certificate = null;
using (var certStream = typeof(T).Assembly.GetManifestResourceStream(resourceName))
{
using (var memory = new MemoryStream((int)certStream.Length))
{
certStream.CopyTo(memory);
certificate = new X509Certificate2(memory.ToArray(), password);
}
}
这个错误只发生在windows server IIS中,但是如果直接运行Kestrel,在IIS Express中没有问题。
它将文件作为非托管内存读取。
解决方案
我遇到了同样的问题,添加 X509KeyStorageFlags.MachineKeySet 就可以了。
certificate = new X509Certificate2(memory.ToArray(), password, X509KeyStorageFlags.MachineKeySet);
在这里找到解决方案:
https://github.com/dotnet/corefx/issues/27358#issuecomment-385433985,
推荐阅读
- python - 我正在使用 > conda update - -all 将所有 anaconda 更新到最新版本,这个错误即将到来
- python - 多个向量之间的距离
- git - git打开与github的连接非常慢......在显示错误消息“致命:遇到TaskCanceledException。任务已取消”之后。
- excel - 使用财务年度设置数据透视表
- pandas - Pandas DataFrame 列,计数器按另一列中的值增加
- javascript - 我如何将类型 datetime-local 转换为字符串
- c# - 如何序列化列表
- > 在 C#/Unity 中转换为 JSON
- javascript - Jquery通过过滤表实时搜索
- c - C - SIGINT 处理程序不能与多个线程一起使用,每个线程都有一个 popen 进程
- mysql - 如何使用 MySQL NOT EXISTS 运算符