本文实例讲述了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#程序设计有所帮助。
总结以上是内存溢出为你收集整理的C#加密app.config中连接字符串的方法全部内容,希望文章能够帮你解决C#加密app.config中连接字符串的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)