If you look at the code below it is doing nothing but creating a new instance of an RSACryptoServiceProvider
.
The key container name is initialized from a property that creates a name based on various parameters. I have added hardcoded values in this demo code.
The code is running on a Windows 2008 R2 server installation and has worked for months with a constant value being returned for the key container name.
Some days ago the code stopped working and we're facing the exception below. Using the key container name that has been used for months no longer works. The server has been rebooted, IIS has been restarted - no success. Only after changing the key name it has started to work again.
Can somebody explain why this is happening and how to fix it? As far as I can see, this code does not crete any persistent objects. Why would it still fail after a reboot? From MSDN (http://msdn.microsoft.com/de-de/library/ca5htw4f.aspx) I read that the constructor "constructor creates or reuses a key container specified using the KeyContainerName field". Does the "reuse" mean, it is caching the stuff somewhere and while doing that it crashed and is now stuck with a corrupt cached version? Also note that the same key name is currently still used on many other machines - no problems anywhere.
This is the line that is crashing:
using ( RSACryptoServiceProvider rsa = new RSACryptoServiceProvider( this.oCspParameters ) )
{
}
These are the CspParameters used:
private readonly CspParameters oCspParameters = new CspParameters
{
Flags = CspProviderFlags.UseMachineKeyStore,
};
this.oCspParameters.KeyContainerName = oProfile.KeyName;
And that's the key name:
public string KeyName
{
get
{
return string.Format( "API-{0}-v{1}", "TestClient", "1.0.0.0" );
}
}
And finally the exception:
CryptographicException: An internal error occurred.
Service Operation: ISessionService.Identify #f173250b-d7ac-45d5-98ed-7fffcf37d95a
at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
at System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
at System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
at System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)