C#加密app.config中連接字符串的方法
更新時間:2015年07月18日 15:10:27 作者:鑒客
這篇文章主要介紹了C#加密app.config中連接字符串的方法,涉及C#配置文件加密的相關(guān)實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了C#加密app.config中連接字符串的方法。分享給大家供大家參考。具體如下:
連接字符串中包含數(shù)據(jù)庫的訪問信息,帳號和密碼,因此一般不以明文顯示,本代碼用來加密連接字符串。
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#程序設(shè)計有所幫助。
相關(guān)文章
C# 使用Fiddler捕獲本地HttpClient發(fā)出的請求操作
這篇文章主要介紹了C# 使用Fiddler捕獲本地HttpClient發(fā)出的請求操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10C#使用AForge實現(xiàn)調(diào)用攝像頭的示例詳解
AForge是一個專門為開發(fā)者和研究者基于C#框架設(shè)計的,這個框架提供了不同的類庫和關(guān)于類庫的資源,本文為大家介紹了C#使用AForge實現(xiàn)調(diào)用攝像頭的相關(guān)教程,需要的可以了解下2023-11-11C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn)
這篇文章介紹了C#循環(huán)與循環(huán)控制的表達式樹實現(xiàn),文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01