如何在 C# 连接字符串中隐藏密码?
我有以下连接字符串:(
Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password
.net webservice) 显然,只需打开 app.config 文件并查看配置设置即可查看这一点。
我需要的是一种让黑客无法看到密码的方法。但同时,使其可自定义,以便在部署到另一个数据库时可以对其进行更改。
I have the following connection string:
Data Source=Paul-HP\MYDB;Initial Catalog=MyMSDBSQL;Persist Security Info=True;User ID=sa;Password=password
(.net webservice)
This can obviously be viewed simply by opening up the app.config file and looking at the configuration settings.
What I need is a way to make a hacker unable to see the password. But at the same time, leave it customisable so that it can be changed when deployed on another database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您有许多选项 - 我所知道的选项(按优先顺序排列):
例如连接字符串可能看起来像这样:
我会选择选项 1,如果不可能,则选择选项 2。为了完整性,我已经提到了选项 3。
您是否阅读过保护连接信息 (ADO.NET)?
You have a number of options - the ones that I am aware of (in order of preference):
So for example the connection string might look like this:
I'd go for option 1, if thats not possible then option 2. I've mentioned option 3 for completeness.
Have you read Protecting Connection Information (ADO.NET)?
首先,不要使用“SA”帐户。如果有人获得密码,您的数据库就会处于开放状态。使用仅允许对特定数据库执行 CRUD 操作的自定义帐户。
获取
web.config
的唯一方法是破解您的服务器。如果他们这么做了,你就完蛋了。First of all, don't use the "SA" account. It leaves your database wide open if someone gets the password. Use a custom account which only is allowed to do CRUD operations on a specific database.
The only way to get
web.config
is to hack your server. And if they have done that, you're screwed anyway.加密 web.config 或 app.config 中的连接字符串可能是最简单的方法,
请参阅如何操作:使用 DPAPI 加密 ASP.NET 2.0 中的配置节
Probably easiest to encrypt the connection strings within the web.config or app.config
See How To: Encrypt Configuration Sections in ASP.NET 2.0 Using DPAPI
我建议加密/解密连接字符串。因此必须手动设置连接字符串。
对于加密,请看一下:
http://dotnet-snippets.de/dns/encrypt-and -decrypt-strings-SID205.aspx
对于自定义设置,请查看:
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
在运行时将 Encrypted 替换为正确的:
I Suggest en/decrypting the connection string. Therefore the connection string has to be set manually.
For encryption take a look at:
http://dotnet-snippets.de/dns/encrypt-and-decrypt-strings-SID205.aspx
For Custom Settings take a look at:
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
Replace the Encrypted with the correct one at runtime:
您可以加密连接字符串 - 然后当您访问连接字符串时,将其解密。但这并不是万无一失的,因为您随后会遇到在哪里存储用于解密连接字符串的密钥的问题!
You could encrypt the connection string - then when you access the connection string, decrypt it. This isn't fool proof though as you're then stuck with the problem of where to store the key to decrypt the connection string!