Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 30,289 times

Related Categories

Encrypting Web.config sections in ASP.NET 2.0

jxlarrea

If you suffer from deep paranoia like me, you'll find a little disturbing to declare all your connection strings in the new <connectionsStrings> section of your web application's Web.config file. This is how it looks like before encrypting:

<connectionStrings>
  <add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs"
    providerName="System.Data.SqlClient" />
  <add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind"
    providerName="System.Data.SqlClient" />
</connectionStrings>

Behold ASP.NET 2.0 new security features. Now you can actually encrypt any section of your Web.config file on-the-fly and programatically. If you have full access to your Web server, you can encrypt your connection strings with this single command-line located in the in the %windows%\Microsoft.NET\Framework\versionNumber folder:

aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"

If you can't execute commands in your web server, for example, when using shared hosting, you still can encrypt it programatically:

Configuration config = Configuration.GetWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.Sections["connectionStrings"];
section.ProtectSection ("DataProtectionConfigurationProvider");
config.Update();

Now, the section in your Web.config file will look like this:

<connectionStrings>
  <EncryptedData>
    <CipherData>
      <CipherValue>AQAAANCMndjHoAw...</CipherValue>
    </CipherData>
  </EncryptedData>
</connectionStrings>

I hope you found this article useful. Happy coding!

Comments

  • Re: Ecryption of the web.config

    Posted by unRheal on 12 Oct 2006

    I noticed you found your own answer - and if anyone else comes here looking for the answer, they can see your solution at:


    Ecryption of the web.config

    Posted by n8nt on 27 Jan 2006

    I have tried this on 3 of my websites and it works on 2 of them but I cannot figure out why it won't work on the 3rd one. I did it using the aspnet_regiis command line. I keep getting an error sayin...