Encrypting the Connection Strings in App.Config file
You need to follow the following steps for encrypting the Connection Strings in the App.Config file.
1. Renaming App.Config file to Web.Config
The aspnet_regiis.exe Command Line Utility of the Visual Studio does not recognize for App.Config files and hence we need to temporarily rename the App.Config file to Web.Config.
2. Encrypting the Connection String in App.Config using aspnet_regiis.exe tool
In order to encrypt the ConnectionString section in the App.Config file, we will need to use the aspnet_regiis.exe tool. the syntax is
aspnet_regiis.exe -pef "connectionStrings" <Path of the Folder containing the App.Config file>
Action – It notifies the action to be performed. In order to perform Encryption, the parameter value is -pef.
Section Name – The name of the section of the App.Config file to be encrypted. For this case, the value will be connectionStrings.
Path of the folder – Here we need to specify the path of the folder containing the App.Config file.
For example, the following command will encrypt all the Connection Strings present in the ConnectionStrings section
aspnet_regiis.exe -pef "connectionStrings" D:\Projects\MyTestApp
3. Renaming Web.Config file to App.Config
Once the encryption is successful, you can now rename the Web.Config file to App.Config so that it can be used in the Windows or Console applications.
4. Decrypting the Connection String in App.Config using aspnet_regiis.exe tool
In order to decrypt the ConnectionString section in the App.Config file, you will have to again rename the App.Config file to Web.Config and once the decryption is successful change its name it back to App.Config.
Syntax
aspnet_regiis.exe -pdf "connectionStrings" <Path of the Folder containing the App.Config file>
Action – It notifies the action to be performed. In order to perform Decryption, the parameter value is -pdf.
Section Name – The name of the section of the App.Config file to be decrypted. For this case the value will be connectionStrings.
Path of the folder – Here we need to specify the path of the folder containing the App.Config file.
Example
aspnet_regiis.exe -pdf "connectionStrings" D:\Projects\MyTestApp