Suwandy
Suwandy

Reputation: 45

Encrypt App or Web.config using aspnet_regiis - Section 'xyz' not found

My app.web.config has custom config section NameValueSectionHandler, but aspnet_regiis cannot find it.

I need to deploy my WPF app to multiple machines with its app.config encrypted. I already tried many walkthrough with aspnet_regiis but nothing works. I tried:

  1. Rename app.config to app.web.config
  2. create public key container aspnet_regiis -pc LiteContainer -exp
  3. I'm stuck at encrypting the custom config section aspnet_regiis -pef connectionSettings D:\Tes -prov LiteProvider

The error is

"The configuration section 'connectionSettings' was not found".
Failed!

But I successfully can read/write data to this section by code.

App/Web.config

<configuration>
    <configSections>
      <section name="connectionSettings" type="System.Configuration.NameValueSectionHandler"/>

      <sectionGroup name="userSettings" .... </sectionGroup>
    </configSections>

    <connectionSettings>
      <server>192.168.1.xxx</server>
      <database>myDb</database>
      <uid>root</uid>
      <pwd>123</pwd>
    </connectionSettings>

  <configProtectedData>
    <providers>
      <add name="LiteProvider"
           keyContainerName="LiteContainer"
           useMachineContainer="true"
           description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
           type="System.Configuration.RsaProtectedConfigurationProvider/>
    </providers>
  </configProtectedData>
</configuration>

I haven't seen any walkthrough encrypt NameValueSectionHandler before, many used applicationSettings or connectionStrings. What am I missing here?

enter image description here

Upvotes: 1

Views: 2282

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65594

I think your command is wrong, even if the folder D:\Tes contains your web.config:

aspnet_regiis -pef connectionSettings D:\Tes -prov LiteProvider

You've mis-typed connectionSettings instead of the connectionStrings:

%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" <full path to directory containing web.config file>

isn't the syntax aspnet_regiis -pef [section name] [web.config path] ? the section name is connectionSettings not connectionStrings


Here is the result when I try it on my PC.

  1. Copy an App.Config with AppSettings (or ConnectionStrings) sections to C:\Temp, and rename it to Web.config.

  2. Run this command: %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "appSettings" c:\Temp

enter image description here

After running the aspnet_regiis command the appSettings is encrypted:

enter image description here

  1. Rename the C:\Temp\Web.Config to App.Config

Solution

Your XML isn't the format expected, eg:

<server>192.168.1.xxx</server>
  <database>myDb</database>
  <uid>root</uid>

Use the standard appSettings or connectionStrings format:

<appSettings>
    <add key="server" value="192.168.1.xxx"/>
    <add key="database" value="myDb"/>
    <add key="uid" value="root"/>
    <add key="pwd" value="123"/>
</appSettings>

REF: https://social.msdn.microsoft.com/Forums/windows/en-US/3b5a1d1f-aa57-40d8-8607-fee0b2a8a6db/protect-appconfig-file-or-encrypt?forum=winforms

https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.appsettings?view=netframework-4.7.2

https://learn.microsoft.com/en-us/dotnet/api/system.configuration.configurationmanager.connectionstrings?view=netframework-4.7.2

Upvotes: 2

Related Questions