How to encrypt .config file Asp.net

2019-09-03 18:39发布

问题:

I want to encrypt common.config, not the Web.config file. Common.config is in a different location than the web.config. The web.config file is connected to the common.config.

web.config:

<appSettings file="C:\Users\ja\Documents\common.config">
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>

common.config:

<appSettings>
    <add key="myKey" value="This is the Value"/>
</appSettings>`

This works for the web.config file:

aspnet_regiis.exe -pef appSettings c:\Users\ja\Desktop\test\webapp\webapp -prov "RsaRpotectedConfigurationProvider"

But when I try to encrypt the common.config file:

aspnet_regiis.exe -pef appSettings c:\Users\ja\Document\ -prov "RsaRpotectedConfigurationProvider"

I get this error:

The configuration for physical path 'c:\Users\ja\Document\' cannot be opened

回答1:

I found workaround to solve this issue. Hope it helps.

  1. Rename your common.config to web.config temporarily.

  2. Add configuration as root element to this file. So your common.config will look like as follows.

<configuration>
  <appSettings>
    <add key="myKey" value="This is the Value"/>
  </appSettings>
</configuration>
  1. Run encrypt command.

    aspnet_regiis.exe -pef appSettings c:\Users\ja\Document\ -prov "RsaProtectedConfigurationProvider"

  2. Open encrypted file and remove Configuration tags.

  3. Rename file to Common.config PS: I deleted my original comments posted on this thread.



回答2:

It doesn't look like you supplied the full path:

c:\Users\ja\Document\

vs

C:\Users\ja\Documents\common.config

If the syntax requires the directory, Document is still missing the 's'