Skip to main content

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message:

System.Security.Cryptography.CryptographicException: The system cannot find the file specified.
  at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)
  at System.Security.Cryptography.X509Certificates.X509Utils._LoadCertFromBlob(Byte[] rawData, IntPtr password, UInt32 dwFlags, Boolean persistKeySet, SafeCertContextHandle& pCertCtx)
  at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromBlob(Byte[] rawData, Object password, X509KeyStorageFlags keyStorageFlags)
  at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData, String password, X509KeyStorageFlags keyStorageFlags)

In order to fix the issue, I have to change following IIS application pool setting:

1. Open IIS Mananger
2. Right click on the application pool the web site associated, select "Advanced Settings"
3. In the "Process Model" section, change "Load User Profile" value to true
4. Recycle application pool, the error will be gone.


Comments

Unknown said…
This post practically saved my life! (A bit of an exaggeration, but not much). Thank you very much! It helped solve a huge issue that we were having.
Steve said…
OMG I was about to lose my mind on this. Thanks!!!! Two lives saved.
Unknown said…
MAN, THIRD LIFE SAVED HERE!!!
KingsLee said…
This comment has been removed by the author.
Anonymous said…
fourth life saved.Thanks
Decepticlone said…
Another life saved, sanity intact. Thanks!
jignesh said…
This comment has been removed by the author.