Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Encrypting Passwords in a .NET app.config File

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?)

like image 287
JohnB Avatar asked Oct 06 '11 02:10

JohnB


1 Answers

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:

  • Task Writing
  • An MSBuild Task to run C# code
like image 149
sll Avatar answered Sep 24 '22 23:09

sll