2

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 ?

Muhammad Usman
  • 1,366
  • 5
  • 18
  • 33
StrugglingCoder
  • 4,781
  • 16
  • 69
  • 103
  • possible duplicate question http://stackoverflow.com/questions/17640055/c-sharp-rsacryptoserviceprovider-how-to-check-if-a-key-already-exists-in-contai – Muhammad Usman Sep 11 '15 at 11:52
  • @MuhammadUsman No sir , it is not a duplication . I evidently told that I tried the method listed out there in the answer but that did not work out for me. – StrugglingCoder Sep 11 '15 at 11:54

0 Answers0