JohnB
JohnB

Reputation: 18962

Encrypt App.config file automatically during build process

Currently, my application automatically encrypts its own App.config file the first time the application is launched.

I'm using the ConfigurationManager class as demonstrated in this article:

Now I want the App.config to already be encrypted before the application launches the first time.

Reason why is that I have added an installer to my solution and I don't want an installer that drops an un-encrypted App.config. I want the process to be automated.

(only thing I came up with so far is attaching an .exe to the Post-Build Event, but isn't there a more straight forward way?)

Upvotes: 7

Views: 4762

Answers (2)

brandiware
brandiware

Reputation: 11

You can code the encryption in the same app you want to deploy. Execute it with a main args switch. This way the PostBuild event can run your own app with the switch that encrypts the config file. This way you can keep all functional context of the encryption task dedicated with your app (e.g. if you want to encrypt different sections for different apps). I use precompiler directives in the app to do the encryption only for Releases. I guess doing this in the PostBuild event like suggested above is more efficient.

Upvotes: 1

sll
sll

Reputation: 62484

First Option: Using built in MSBuild Exec Task execute own utility program which is able to encrypt configuration file.

Second Option: Own MSBuild task

I believe you can write a simple MSBuild Task which will find App.Config file and then encrypt it whilst Solution build.

  1. Create a new MSBuild Targets like EncryptConfig.targets
  2. Update csproj file by <Import Project="EncryptConfig.targets" />
  3. Add DependsOnTargets for standard AfterBuild targets in csproj:
 <Target Name="AfterBuild" DependsOnTargets="EncryptConfig.targets" />

4) Write own task and execute it in EncryptConfig.targets

How-to write own task:

Upvotes: 3

Related Questions