Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RSACryptoServiceProvider on Azure web site results in file not found error

I am moving an existing (and working) ASP.NET web site to Azure web site. One of the bits of functionality on the site is signing an XML document. The code to get the key is:

// retrieve a key from the key safe - this will create it if it does not exist yet
System.Security.Cryptography.CspParameters csp = new CspParameters();
csp.KeyContainerName = "MyKeyName";
System.Security.Cryptography.RSACryptoServiceProvider key = new RSACryptoServiceProvider(csp);

The last line is throwing a CryptographicException, with the message "The system cannot find the file specified".

I have not put a key or container into Azure - my understanding is that the ServiceProvider would create one. I have reviewed this article, but did not get any clues.

Clearly I am missing something fundamental.

like image 656
OzDave Avatar asked Nov 20 '14 21:11

OzDave


1 Answers

Thanks Simon - that pointed me in the right direction.

Turns out you need to specify that the key be created in a machine store. Code that worked is:

System.Security.Cryptography.CspParameters csp = new CspParameters();
csp.KeyContainerName = "MyKeyName";
csp.Flags = CspProviderFlags.UseMachineKeyStore;

Note the addition of the line specifying "UseMachineKeyStore"

like image 84
OzDave Avatar answered Nov 02 '22 23:11

OzDave