从asp.net核心资源中读取嵌入文件

时间:2018-06-05 20:23:53

标签: c# asp.net-core-mvc

将我的网站发布到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服务器IIS中,但如果直接运行Kestrel并且在IIS Express中没有问题,则查找。

Break point on stream

接缝文件被读取为非托管内存。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,添加了X509KeyStorageFlags.MachineKeySet可以解决问题。

certificate = new X509Certificate2(memory.ToArray(), password, X509KeyStorageFlags.MachineKeySet);

在这里找到解决方案:

https://github.com/dotnet/corefx/issues/27358#issuecomment-385433985

http://web.archive.org/web/20151101033040/http://blog.tylerdoerksen.com:80/2013/08/23/pfx-certificate-files-and-windows-azure-websites/

https://stackoverflow.com/a/48234395/3080858