CryptographicException – “An internal error occurred”

I had some code that looked something like this…

byte[] certificate;
string certificatePassword;
var certificate = new X509Certificate2(certificate, certificatePassword);

…and it threw a CryptograhicException with the most unhelpful of error messages “An internal error occurred”.

I am running this code within a web-service and thus within IIS – As such the ‘user’ that this code is running under (the app pool user / IIS user) does not have the appropriate permissions to handle the above statement.  This is because the certificate will be temporarily stored in a certificate store, which by default is the user store, but the app pool / IIS user does not have rights to that certificate store.

To solve this issue we simply need to state that the certificate should be handled by the local computer store – by specifying an extra parameter of X509KeyStorageFlags.MachineKeySet.  This then solves the problem.  So, our code from earlier should simply look like below…

var certificate = new X509Certificate2(certificate, certificatePassword, X509KeyStorageFlags.MachineKeySet);

If you come across the same problem and this helps you then that’s great! 🙂

Problem adding a WebService in VS2010

So, I was using Visual Studio 2010 the other day to create a new web-service, when I was presented with the following error message…

Could not find file ‘c:\Program Files (x86)\ Microsoft Visual Studio 10.0\ Common7\ IDE\ ItemTemplatesCache\ CSharp\ Web\ 1033\\ WebService.asmx’.

Could not find file c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\ItemTemplatesCache\CSharp\Web\1033\\WebService.asmx

I tried adding the web-service a couple of times but every time I pressed the ‘Add’ button it would present the above error.

Add Web Service presents could not find file error

So, I looked at the location stated in the error message and sure enough, there was no WebService.asmx at that location…

Missing webservice.asmx

What I found interesting was that the path contained the word ‘cache’, which to me implied the master/install location containing the missing file would be somewhere else on the computer.

So, I had a quick search and found a WebService.Zip in the folder shown below.

I opened up the zip file – it contained the illusive WebService.asmx – so I copied it into the folder mentioned in the error message and hey-presto, it solved the problem!


So, if you ever get this error message then try copying the file from the master/install zip file and see if it fixes your problem.  If it does then let me know!