Pages

Tuesday, February 22, 2011

Encrypt & Decrypt Connection string in web.config

first import System.web.configuration namespace then add folowing code for encryption
for example on some button i have written following code for encryption
        string path=@"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");
        appsetting.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        config.Save();

ProtectSection Method in  SectionInformation encrypts the connection string

Now lets see for how to decrypt, SectionInformation contains method UnprotectSection() for decryption
code as below
        string path = @"\Projectname";
        Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
        ConfigurationSection appsetting = config.GetSection("connectionStrings");
        appsetting.SectionInformation.UnprotectSection();
        config.Save();

This Snippet will help to Encrpt & Decrypt  connection String

No comments:

Post a Comment