HAVIT Knowledge Base

Vývoj webových aplikací, .NET, SQL, návrh
Welcome to HAVIT Knowledge Base Sign in | Join | Help
-
Home Články Forums Obrázky Soubory

.NET Framework

Microsoft .NET Framework, Base Class Library

X509Certificate2: CryptographicException: The system cannot find the file specified.

Pokud vytváříte ve webové aplikaci X509Certificate2

myCertificate = new X509Certificate2(rawData, password);
//nebo
myCertificate = new X509Certificate2(fileName, password);
pak můžete být po nasazení své funkční aplikace na produkční server (např. hosting) obdařeni výjimkou

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)

...přestože soubor s certifikátem buď zaručeně existuje (fileName), nebo se ani nepoužívá (rawData).

 

Problém souvisí s tím, že v produkčním prostředí Vaše aplikace beží pod uživatelským účtem, který nemá založen profil, který se má certifikát vytvořit (volně přeloženo, omlouvám se za případnou nepřesnost).

Řešením je použití přetížení constructoru, kterým zvolíte cílové uložiště certifikátu:

myCertificate = new X509Certificate2(rawData, password, X509KeyStorageFlags.MachineKeySet);
//nebo
myCertificate = new X509Certificate2(fileName, password, X509KeyStorageFlags.MachineKeySet);

Published 20. května 2009 13:38 by Robert Haken
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

What do you think?

(required) 
(optional)
(required) 
Enter the code you see below

Submit