0

After publish my site to iis, i get a error as:

Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException: The system cannot find the file specified

My code to read X509Certificate2 from embedded file is:

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);
    }
}

this error only occurred in windows server IIS, but if run Kestrel directly and in IIS Express no problem find.

Break point on stream

It seams the file is read as unmanaged memory.

alireza.salemian
  • 536
  • 5
  • 21

1 Answers1

0

I had the same issue, adding X509KeyStorageFlags.MachineKeySet did the trick.

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

Found the solution here:

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

Oyvind Habberstad
  • 1,004
  • 11
  • 14