C#加密app.config中连接字符串的方法

C#加密app.config中连接字符串的方法,第1张

概述本文实例讲述了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#加密app.config中连接字符串的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1260870.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存