Một đoạn code nhỏ để bảo vệ chuỗi kết nối dữ liệu
protected void btnEncrypt_Click(object sender, EventArgs e) { EncryptSection("connectionStrings", "RsaProtectedConfigurationProvider"); } protected void EncryptSection(string section, string provider) { // Lấy thông tin file web.config của một website Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); // Tham chiếu tới section cần mã hóa trong file web.config ConfigurationSection cons = config.GetSection(section); // Tiến hành mã hóa cons.SectionInformation.ProtectSection(provider); // Lưu lại thông tin sau khi mã hóa config.Save(); } protected void btnDecrypt_Click(object sender, EventArgs e) { DecryptSection("connectionStrings"); } protected void DecryptSection(string section) { // Lấy thông tin file web.config của một website Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); // Tham chiếu tới section cần mã hóa trong file web.config ConfigurationSection cons = config.GetSection(section); cons.SectionInformation.UnprotectSection(); config.Save(); }