Creating an X509Certificate2 under IIS throws 'The system cannot find the file specified'
This exception had me scratching my head for a good 30 minutes until the power of Bing (yes, Bing!) gave me the answer:
System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
The code I'm using looks a lilttle bit like this:
using (var stream = assembly.GetManifestResourceStream(certificateResourceStreamName))
{
var certificateBytes = ReadStream(stream); X509Certificate2 certificate = new X509Certificate2(certificateBytes, certificatePrivateKey);
return certificate;
}
When this code was run, and the exception thrown, I was flummoxed and ended up adding many lines of diagnostics to try and establish what was going wrong. There's a stackoverflow question for the same exception, with the same stack trace (CryptographicException was unhandled: System cannot find the specified file) but my issue wasn't the same as that which the OP stated theirs was in comments on the question.
In my case, the code is running in IIS under an ApplicationPoolIdentity user and there is a non-standard configuration for the application pool. Here's the Process Model settings for the pool on the server in question:
And here's the same on a server that's not wonky:
As soon as I changed the Load User Profile setting to True, the code started working and I was able to move onto the next problem. There's an answer on the stackoverflow question (that has more up-votes than the rest of the answers on the question, and the question itself, combined) that told me what the problem was. Based on the very high number of up-votes, this is something people are seeing reasonably often!