I was recently following Jouni Heiknieme’s blog post on Encrypting connection strings in Windows Azure web applications when I stumbled across a problem.
The issue was that I wasn’t encrypting the
connectionStrings section, I was encrypting a custom section (one provided by SimpleAuthentication). And in order to encrypt that section,
aspnet_regiis needs access to the DLL that defines the config section. If it cannot find the DLL it needs it will respond with an error message:
C:\dev\Xander.HorribleCards\src\Xander.HorribleCards.UI.Web>aspnet_regiis -pef "authenticationProviders" . -prov "Pkcs12Provider" Microsoft (R) ASP.NET RegIIS version 4.0.30319.18408 Administration utility to install and uninstall ASP.NET on the local machine. Copyright (C) Microsoft Corporation. All rights reserved. Encrypting configuration section... An error occurred creating the configuration section handler for authenticationProviders: Could not load file or assembly 'SimpleAuthentication.Core' or one of its dependencies. The system cannot find the file specified. (C:\dev\Xander.HorribleCards\src\Xander.HorribleCards.UI.Web\web.config line 7) Could not load file or assembly 'SimpleAuthentication.Core' or one of its dependencies. The system cannot find the file specified. Failed!
And here is the relevant part of the web.config file
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=188.8.131.52, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=184.108.40.206, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> <section name="authenticationProviders" type="SimpleAuthentication.Core.Config.ProviderConfiguration, SimpleAuthentication.Core" /> </configSections>
It took searching through a few forum posts before I eventually found the answer. Most were pointing in the right general direction. You either have to load the assembly that defines the config section into the GAC (not possible for me as it was a third party assembly that was not strong named) or put it where
aspnet_regiis was looking for it.
All the non-GAC solutions that I found were hacky horrible things that put the assembly somewhere in the .NET folder.
My problem was that where everyone was saying to put it wasn’t working for me. So I loaded up Process Monitor to look to see where exactly the
aspnet_regiis was looking. It turns out that because I was using the 64bit version of the command prompt I should be looking in
I put the assembly in that directory and the
aspnet_regiis worked and the relevant section was encrypted, it was runnable and I could store it to source control without other people knowing what my secret keys are.
Round tripping the encryption/decryption
I also had some issues round tripping the encrypted and decrypted config file while developing. I kept getting the error message:
Decrypting the relevant config settings Microsoft (R) ASP.NET RegIIS version 4.0.30319.18408 Administration utility to install and uninstall ASP.NET on the local machine. Copyright (C) Microsoft Corporation. All rights reserved. Decrypting configuration section... Failed to decrypt using provider 'Pkcs12Provider'. Error message from the provider: Keyset does not exist (C:\dev\Xander.HorribleCards\src\Xander.HorribleCards.UI.Web\web.config line 65) Keyset does not exist Failed!
It turned out to be a permissions issue on the private key. This post “Keyset does not exist” on Stack Overflow helped on how to resolve that.