2017年8月16日 星期三

加密 Web.config 中 Connection string 的方法



避免設定檔中連線字串部分以明碼顯示




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();
}
}
解密方法

#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
view raw Decrypt.cs hosted with ❤ by GitHub

沒有留言:

張貼留言

區網電腦連 WAMP 架設的網站 出現You don’t have permission to access / on this server 解決辦法

參考 修改 httpd.conf 內容 1. 將 Require all denied 改為 Require all granted 參考2  修改  httpd-vhosts.conf  內容 2.將 Require local 改為 Require ...