.net - app.config 中的连接字符串。安全性如何?

标签 .net connection-string app-config

将带有密码的连接字符串放在 app.config 文件中真的是一件好事吗?

在我看来,app.config 没有以任何方式加密,密码信息可以很容易地读取。

我有一个应用程序可以访问目标最终用户没有身份验证的数据库。使用组用户/密码。该应用程序仅在当前 Windows 用户位于 Active Directory 组中时启动。因此,一旦进入应用程序,用户就可以使用组用户连接到数据库。

处理此类连接字符串的正确方法是什么?将它们隐藏在源代码中?

注意这是针对独立应用程序的 - 不是 ASP、IIS 等

这对我有用

(感谢 Jon Galloway - http://weblogs.asp.net/jgalloway/archive/2008/04/13/encrypting-passwords-in-a-net-app-config-file.aspx)

private void EncryptConfigSection()
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSection section = config.AppSettings;
    if (section != null)
    {
        if (!section.SectionInformation.IsProtected)
        {
            if (!section.ElementInformation.IsLocked)
            {
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Full);
            }
        }
    }
}

这通过在应用程序首次运行时加密 exe 配置文件来实现。我还没有找到在安装过程中执行此操作的方法,因此在应用程序首次启动之前,配置文件是完全可读的。也许有人有想法...

最佳答案

您可以加密部分 app.config 或 web.config 文件,参见 for example this post获取更多信息。

具体来说,this MSDN article介绍了保护连接字符串的各种方法。

关于.net - app.config 中的连接字符串。安全性如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3204288/

相关文章:

c# - 带有 App.config 文件的 SQL Server 外部的 connectionString

c# - 在 .NET 4.6 项目中使用 Google.Protobuf

c# - 如何在遇到第二个空格之前从字符串中提取子字符串?

asp.net - 使用 Azure Cloud 时,Web.config 中的 DB ConnectionString 是否应该加密?

c# - 验证 App.Config 中的值

c# - app.configs 和 MSTest 项目 - 连接字符串的空引用

c# - 方法返回一个接口(interface)

.NET 进程内存使用 = 5x CLR 堆内存?

asp.net - DefaultConnection 和成员资格 - localsqlserver 和 defaultconnection 之间的连接是什么

c# - 如何从实体连接字符串中获取数据源、用户 ID 和密码?