Skip to content Skip to sidebar Skip to footer

Encrypt Connection String In C# Code

I have connection string in my c# code, so i can get data from database using a stored procedure. The connection string is like this: connection.ConnectionString = 'Data Source=192

Solution 1:

You can use this :

try
{
    // Open the configuration file and retrieve // the connectionStrings section.Configurationconfig= ConfigurationManager.
        OpenExeConfiguration(exeConfigName);

    ConnectionStringsSectionsection=
        config.GetSection("connectionStrings")
        as ConnectionStringsSection;

    if (section.SectionInformation.IsProtected)
    {
        // Remove encryption.
        section.SectionInformation.UnprotectSection();
    }
    else
    {
        // Encrypt the section.
        section.SectionInformation.ProtectSection(
            "DataProtectionConfigurationProvider");
    }
    // Save the current configuration.
    config.Save();

    Console.WriteLine("Protected={0}",
        section.SectionInformation.IsProtected);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}

or you Enterprise Library Data Access Application Block to perform the encryption using RSAProtectedConfigurationProvider or DPAPIProtectedConfigurationProvider. the link: http://msdn.microsoft.com/en-us/library/89211k9b(VS.80).aspx

Post a Comment for "Encrypt Connection String In C# Code"