我有以下连接字符串:
Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password
(.net webservice) 这显然可以通过打开 app.config 文件并查看配置设置来查看。
我需要的是一种让黑客无法看到密码的方法。但同时,让它可定制,以便在部署到另一个数据库时可以更改它。
我有以下连接字符串:
Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password
(.net webservice) 这显然可以通过打开 app.config 文件并查看配置设置来查看。
我需要的是一种让黑客无法看到密码的方法。但同时,让它可定制,以便在部署到另一个数据库时可以更改它。
你有很多选择——我知道的那些(按优先顺序排列):
例如,连接字符串可能如下所示:
Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID={0};Password={1}
我会选择选项 1,如果那不可能,那么选项 2。我已经提到了选项 3 的完整性。
你读过保护连接信息(ADO.NET)吗?
首先,不要使用“SA”账户。如果有人获得密码,它会使您的数据库完全打开。使用只允许对特定数据库执行 CRUD 操作的自定义帐户。
获得的唯一方法web.config
是破解您的服务器。如果他们这样做了,那么无论如何你都被搞砸了。
可能最容易加密 web.config 或 app.config 中的连接字符串
我建议对连接字符串进行加密/解密。因此必须手动设置连接字符串。
对于加密看看: http ://dotnet-snippets.de/dns/encrypt-and-decrypt-strings-SID205.aspx
对于自定义设置,请查看:http: //msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
在运行时将 Encrypted 替换为正确的:
public static void SetAppSettingValue(string Key, string Value)
{
System.Configuration.Configuration config == ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings[Key].Value = Value;
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
您可以加密连接字符串 - 然后当您访问连接字符串时,解密它。这不是万无一失的,因为您随后会遇到存储密钥以解密连接字符串的问题!