Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically encrypting a config-file in .NET

Could somebody please do a rundown of how to programmatically encrypt a config-file in .NET, preferably in C#.

What I would like to do is do some kind of check on an application's startup to see if a section is unprotected, and if it is, then encrypt it. This for both settings and connection-strings.

Also if anyone could list the types of encryption-providers and what is the difference between them.

I don't know if the code for doing this in a normal WinForms-application is transparent to doing this in ASP.NET.

like image 483
Seb Nilsson Avatar asked Aug 22 '08 09:08

Seb Nilsson


People also ask

How do I encrypt configuration files in the web framework?

The .NET Framework 2.0 allows you to encrypt most configuration sections within the Web.config or machine.config files. The System.Web.Configuration namespace is used to encrypt configuration files via code.

How to encrypt and decrypt configuration information programmatically in the NET Framework?

The code, as we will see shortly, simply needs to programmatically reference the appropriate configuration section, call its ProtectSection or UnprotectSection method, and then call the Save method to persist the changes. Moreover, the .NET Framework includes a helpful command line utility that can encrypt and decrypt configuration information.

How to encrypt the configuration part of an ASPnet service?

The aspnet_regiis will only encrypt configuration sections - so you need to selectively encrypt those parts you need, like this: cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 aspnet_regiis.exe -pef "system.serviceModel/bindings" . aspnet_regiis.exe -pef "system.serviceModel/services" .

Is it possible to encrypt data in ASP NET?

While a user will have a hard time dealing with encrypted data, ASP.NET has no problems. You can use both of these providers in ASP.NET code. The .NET Framework 2.0 allows you to encrypt most configuration sections within the Web.config or machine.config files. The System.Web.Configuration namespace is used to encrypt configuration files via code.


1 Answers

To summarize the answers and what I've found so far, here are some good links to answer this question:

  • Encrypting Configuration Information in ASP.NET 2.0 Applications - 4GuysFromRolla.com
  • How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI - MSDN

Please feel free to complement with other links, maybe some to WinForms- or WPF-applications.

like image 125
Seb Nilsson Avatar answered Sep 25 '22 20:09

Seb Nilsson