【问题标题】:How to prevent reading app.config?如何防止读取 app.config?
【发布时间】:2013-09-28 03:20:17
【问题描述】:

抱歉,这看起来是个愚蠢的问题。

我的应用程序使用连接字符串连接到 SQL Server 2008 数据库并使用 Crystal Report,我的服务器使用混合身份验证模式。

问题是:app.config 文件显示了我不想让任何人看到的连接字符串(用户名和密码)!

Data Source=.\SQLEXPRESS;Initial Catalog=ComplainsDb;Persist Security Info=false; User ID=abcde ;Password=MyPassword

感谢您的帮助。

【问题讨论】:

  • 加密 app.config。谷歌它你会发现很多关于它的..
  • 相关question

标签: c# sql-server-2008 crystal-reports app-config


【解决方案1】:

当你的程序启动时,你需要这样调用:

    void EncryptConnectionStringsIfNecessary()
    {
        var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ConnectionStringsSection section = configFile.ConnectionStrings;
        if (section != null)
        {
            if (!section.IsReadOnly())
            {
                if (!section.SectionInformation.IsProtected)
                {
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                    section.SectionInformation.ForceSave = true;
                    configFile.Save(ConfigurationSaveMode.Full);
                }
            }
        }
    }

【讨论】:

  • 到目前为止这对我有用.. 但是我怎样才能解密这个文件??
  • 您通常不需要做任何事情。如the MSDN documentation 中所述,当您访问ConfigurationManager.ConnectionStrings[connectionStringName] 时,.NET 框架会自动解密设置。
  • 非常感谢每一位帮助我的人
  • 仅供参考:ConfigFile 应该是 configFile。 ;)
  • 谢谢@JamesWilkins。已更新。
猜你喜欢
  • 2012-07-30
  • 2014-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-28
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
相关资源
最近更新 更多