How to add password to Web.config and encrypt it

I've been all over Google, MSDN, and Stack Overflow, but I can't find a complete tutorial on how to do this.

I want to add a password to my Web.config file for an ASP.NET web application and I want to encrypt it. How to do it?

+3


source to share


1 answer


How to add a Web.config section and encrypt it.

You want to add a partition because it aspnet_regiis

will encrypt partitions in one go, and you probably have configuration settings that you don't want to encrypt.

  • Don't use App.config in your project. I couldn't get it to work. You will get this error message: The configuration section 'secureAppSettings' was not found

    . Use Web.config in your main web project.
  • Create a section in your Web.config like this:

Web.config



<configSections>
  <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler" />
</configSections>

      

  1. Customize your new section

Web.config



<secureAppSettings>
  <add key="Password" value="1234567890"/>
</secureAppSettings>

      

  1. You can create a section group. But this is optional.
  2. The type of section is important. AppSettingsSection

    didn't work for me, but NameValueSectionHandler did.
  3. Here's the code to get the setting from the section:

C # code

string p = ((NameValueCollection)WebConfigurationManager.GetSection("secureAppSettings"))
           ["Password"];

      

  1. To encrypt, run Start> Programs> Visual Studio> Visual Studio Tools> Developer Command Prompt As Administrator

    .
  2. Note that the following command has case sensitive strings and you may need to enclose your project path in quotes. The Web.config must be INSIDE this folder, so do not include Web.config in the command.
  3. aspnet_regiis -pef "secureAppSettings" "C:\folder\with\the_config_file"

  4. You will notice that the section in your Web.config file is now encrypted!

If you are using a section like Web.config, System.Configuration.AppSettingsSection

you are likely to run into this error. Could not load type 'System.Configuration.AppSettingsSection' from assembly 'System.Web'

I have not been able to get AppSettingsSection to work.

+5


source







All Articles