How to Encrypt and Decript web.config connection string?

How to Encrypt and Decript  web.config connection string?

web.config
<connectionStrings>
   <add name="connection" connectionString="data source=localhost;initial catalog=test;uid=sa;pwd=abcxyz"   providerName="System.Data.SqlClient" />
 </connectionStrings>


Add Two Button Encrypt and Decript  in asp.net and  write the code such as  
protected void btnEncript_Click(object sender, EventArgs e)
    {
        Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(GetWebConfigMappedPath(), "/");
        ConfigurationSection appSetting = config.GetSection("connectionStrings");
        appSetting.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
        config.Save();  
    }
    protected void btnDecript_Click(object sender, EventArgs e)
    {
        Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(GetWebConfigMappedPath(), "/");
        ConfigurationSection appSetting = config.GetSection("connectionStrings");
        appSetting.SectionInformation.UnprotectSection();
        config.Save();
    }

    private WebConfigurationFileMap GetWebConfigMappedPath()
    {
        string webConfigPath = Server.MapPath("Web.config");
        FileInfo configFile = new FileInfo(webConfigPath);
        VirtualDirectoryMapping virtualpath = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
        WebConfigurationFileMap webconfigMap = new WebConfigurationFileMap();
        webconfigMap.VirtualDirectories.Add("/", virtualpath);
        return webconfigMap;
    }

Comments

Popular posts from this blog

Display multiple marker point on Google Map with same location or same Longitude,Latitude

Implementing Zoom functionality in asp.net

Check Session Timeot for Whole Application