Search code examples
asp.netvb.netprivate-key

CryptographicException "Object was not found" opening key store


I'm stymied by an error I'm getting trying to open a p12 keystore. This works fine in development but when I compile and move to a production server, an "Object was not found" error is thrown.

About all I know about this is that it's not because the keystore file doesn't exist. When I remove the store from the server, the error message changes to "The system cannot find the file specified."

Any ideas what the problem could be or how to debug? Unfortunately I cannot step through because this is compiled code.

Here's the trace:

System.Security.Cryptography.CryptographicException: Object was not found.

   at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromFile(String fileName, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(String fileName, String password)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName, String password)

Solution

  • Solved the problem soon afterward. Here is the resolution:

    • On IIS, open application pool advanced settings.
    • Set Load User Profile to TRUE
    • Set Identity to LocalService
    • Recycle application pool