While encrypting web.config using aspnet_regiis tool the key conatiner gets stored in the MachineKeys folder that is C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys.
How could I check whether a key container having certain name exists already or not ? That is whether the config is being successfully encrypted for the very first time or not ?
Apparently I tried
public static bool DoesKeyExists(string containerName)
{
var cspParams = new CspParameters
{
Flags = CspProviderFlags.UseExistingKey,
KeyContainerName = containerName
};
try
{
var provider = new RSACryptoServiceProvider(cspParams);
}
catch (Exception e)
{
return false;
}
return true;
}
and the call to the method was like
if (!DoesKeyExists("MyKeys"))
{
p.StandardInput.WriteLine("aspnet_regiis.exe -pc \"MyKeys\" -exp");
p.StandardInput.WriteLine("aspnet_regiis.exe -pa \"MyKeys\" \"NT AUTHORITY\\NETWORK SERVICE\"");
p.StandardInput.WriteLine("aspnet_regiis.exe -pa \"MyKeys\" \"IIS APPPOOL\\ApplicationPoolName\"");
p.StandardInput.WriteLine("aspnet_regiis -pef \"connectionStrings\" {0} -prov \"RSAProtectedConfigurationProvider\"", strEntry);
p.StandardInput.WriteLine("aspnet_regiis -px \"MyKeys\" {0} -pri", KeyFileName);
}
but even after creating the key container at the very first run itself it says the Key Set does not exist and throws error and in turn returns false for the subsequent runs.
What should be done ?