手机
当前位置:查字典教程网 >编程开发 >C#教程 >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#程序设计有所帮助。

【C#加密app.config中连接字符串的方法】相关文章:

C#中字符串编码处理

C#图片压缩的实现方法

C#中字符串的加密的源码

C#中使用split分割字符串的几种方法小结

C#连接Oracle数据库的实例方法

C#中隐式运行CMD命令行窗口的方法

C#.NET字符串比较中忽略符号的方法

C# 判断字符串为空的几种办法

c#中分割字符串的几种方法

c# 获取数据库中所有表名称的方法

精品推荐
分类导航