We have a code that creates a new X509Certificate2 and it works perfectly in development environment. The same code crashes in IIS hosting to the following stack trace:
An unhandled exception was thrown by the application.
System.Security.Cryptography.CryptographicException: Cannot find the requested object.
at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertBlobType(Byte[] rawData)
at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData)
At first I thought we provide empty cert to the constructor, but looking at the source code tells me that there should be a bit different error message if the rawData would be NULL or empty.
Edit 1
I managed to get pass the problem by installing the certificate manually to the server and then just getting it from the storage instead of creating it on the fly. Still don't know why the creating fails.