Encrypt & Decrypt ConnectionString Section
Sometimes we need to secure ConnectionString to prevent anyone can knows it. whatever your purpose from securing ConnectionString, there is a way to Encrypt and Decrypt ConnectionString by special codes as we will see now...
In the Web.Config file we found <connectionStrings> section that enable us to add ConnectionStrings
<connectionStrings>
<add name="ConnectionString" connectionString="Provider=SQLNCLI10.1;Data
Source=My-pc\sqlexpress;Integrated Security=SSPI;Initial Catalog=Northwind"
providerName="System.Data.OleDb" />
</connectionStrings>
Now we have to go to see how can we Encrypt and Decrypt ConnectionStrings don't foreget add System.Web.Configuration name space
protected void Encryption(bool EncryptoValue)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection sec = config.GetSection("connectionStrings");
if (EncryptoValue == true)
{
sec.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
else
{
sec.SectionInformation.UnprotectSection();
}
config.Save();
}
protected void Button1_Click(object sender, EventArgs e)
{
Encryption(true);
}
protected void Button2_Click(object sender, EventArgs e)
{
Encryption(false);
}
You can test that now hope that useful
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.
The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.