OzDave
OzDave

Reputation: 164

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.

Upvotes: 6

Views: 1361

Answers (1)

OzDave
OzDave

Reputation: 164

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"

Upvotes: 8

Related Questions