Contact Us

Scripts: Asp :: encryption :: Library Article #7

Developer's Section

Encrypt / Decrypt Database Parameters in ASP.NET
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3036)

Learn to encrypt database parameters in ASP.NET

You can take an extra security measure in your ASP.NET applications when database login information needs to be protected. The best way is to use the following code: 1.Open the web.config file in your ASP.NET application:

 Code Snippet 1

    <add name="DatabaseParameters1" 
    providerName="System.Data.SqlClient" />
    <compilation debug="true"/>
      <authentication mode="Windows"/>
    <pages theme="Theme1" />

Now, open the file of the current form you are working on and add the following to the code behind of the file:

 Code Snippet 2

using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;
public void EncryptConnString()
  Configuration config = WebConfigurationManager.OpenWebConfiguration(
  ConfigurationSection section = config.GetSection("connectionStrings");
  if (!section.SectionInformation.IsProtected)

The RSAProtectedConfigurationProvider model provided will then encrypt the connection string when the script is executed.

In a similiar manner you can use the DataProtectionConfigurationProvider to decrypt connection strings:

 Code Snippet 3

public void DecryptConnString()
  Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
  ConfigurationSection section = config.GetSection("connectionStrings");
  if (section.SectionInformation.IsProtected)

See other Scripts in encryption

Submit Your Scripts:

System Message:
  • Your ASP & ASP.NET script has been sent. Please allow 48 hours for processing.

If you would like to have your ASP & ASP.NET scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]