RSACryptoServiceProvider CryptographicException System Cannot Find the File Specified under ASP.NET

I fixed this by setting "Load User Profile" to True (was False) in the Application Pool's Advanced Settings / Process Model section.

The application had been working perfectly on Server 2003 R2 / IIS 6 and the problem appeared as I was configuring it on our new 2008 R2 server.

Got the idea to try it at:

http://social.msdn.microsoft.com/forums/en-US/clr/thread/7ea48fd0-8d6b-43ed-b272-1a0249ae490f/

YMMV


Indeed you need to work a code like this

CspParameters _cpsParameter;
RSACryptoServiceProvider RSAProvider;

_cpsParameter = new CspParameters();
_cpsParameter.Flags = CspProviderFlags.UseMachineKeyStore;

RSAProvider = new RSACryptoServiceProvider(1024, _cpsParameter); 

The following users need access to the folder: C:\Documents and Settings\All Users\Application data\Microsoft\Crypto\RSA\MachineKeys

  1. IIS user account (anonymmous)
  2. The user account you use to impersonate your application in the web.config settings.

So now it is working fine for me.


Try setting

System.Security.Cryptography.RSACryptoServiceProvider.UseMachineKeyStore = true;

EDIT: Then try using

var provider = new System.Security.Cryptography.RSACryptoServiceProvider();

instead of the constructor with the integer-parameter. That constructor tries to generate a key with the specified key-length, and you might not be able to do that with your permissions.