C#加密app.config中銜接字符串的辦法。本站提示廣大學習愛好者:(C#加密app.config中銜接字符串的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#加密app.config中銜接字符串的辦法正文
本文實例講述了C#加密app.config中銜接字符串的辦法。分享給年夜家供年夜家參考。詳細以下:
銜接字符串中包括數據庫的拜訪信息,帳號和暗碼,是以普通不以明文顯示,本代碼用來加密銜接字符串。
public static class EncryptConnection
{
public static void EncryptConnectionString(bool encrypt)
{
Configuration configFile = null;
try
{
// Open the configuration file and retrieve the connectionStrings section.
configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection configSection = configFile.GetSection("connectionStrings") as ConnectionStringsSection;
if ((!(configSection.ElementInformation.IsLocked)) && (!(configSection.SectionInformation.IsLocked)))
{
if (encrypt && !configSection.SectionInformation.IsProtected)
//encrypt is false to unencrypt
{
configSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
if (!encrypt && configSection.SectionInformation.IsProtected)
//encrypt is true so encrypt
{
configSection.SectionInformation.UnprotectSection();
}
//re-save the configuration file section
configSection.SectionInformation.ForceSave = true;
// Save the current configuration.
configFile.Save();
}
}
catch (System.Exception ex)
{
throw (ex);
}
finally
{
}
}
}
願望本文所述對年夜家的C#法式設計有所贊助。