Wednesday, May 6, 2015

Encrypt and Decrypt Web.config File

For Encryption:


Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection configSection =config.GetSection("connectionStrings");
            if (!configSection.SectionInformation.IsProtected)
            {
                configSection.SectionInformation.
                    ProtectSection("DataProtectionConfigurationProvider");
                config.Save();
                Response.Write("ConnectionStrings has been encryted successfully.");
            }
            else
            {
                Response.Write("ConnectionStrings has been encryted, this action has been cancled");
            }





For Decryption:

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
            ConfigurationSection configSect = config.GetSection("connectionStrings");
            if (configSect.SectionInformation.IsProtected)
            {
                configSect.SectionInformation.UnprotectSection();
                config.Save();
                Response.Write("ConnectionStrings has been Decrypted successfully.");

            }

No comments:

Post a Comment