避免設定檔中連線字串部分以明碼顯示
path = Process.GetCurrentProcess().MainModule.FileName;
public static void EncryptConnString(string path)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
config.Save();
}
}
#endregion
解密方法
#region Decrypt method
public static void DecryptConnString(string path)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
}
#endregion