在 App.config 中加密密码
我想加密连接字符串中的密码。当我连接到数据库时,连接字符串公开存储在 App.config 中,我需要找到一种方法来仅加密密码。
I want to encrypt the password in connection string. When I make a connection to DB the connection string is openly stored in App.config and I need to find a way to keep only password encrypted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设这是您的连接字符串:
那么您可以执行以下操作:
您可以使用此处的示例编写
EncDecHelper.Decrypt
:加密和解密字符串Lets say this is your connection string:
Then you can do something like this:
You can write
EncDecHelper.Decrypt
by using samples from here: Encrypt and decrypt a string使用
connectionStrings
配置部分并加密整个部分 - 而不仅仅是密码。这更安全,因为您的应用程序配置将不再具有纯文本形式的服务器名称和用户名。
MSDN 上有关于 RSA 或DPAPI。
Use the
connectionStrings
configuration section and encrypt the whole section - instead of just the password.This is safer as your app config will no longer have the server names and user names in plain text either.
There are how-to documents for encrypting configuration sections on MSDN for RSA or DPAPI.
也许在加载应用程序之前从您的配置中解密连接字符串。
Maybe decrypt connection string from your config before application was loaded.
作为其他答案的补充,使用源代码管理中的文件作为模板,仅使用开发/测试加密的连接字符串,以便它在开发/测试中工作不是更好吗?
对于生产(或应用程序部署到的其他环境),加密的凭据文件会按照指定的模板格式单独生成,单独管理/更新/部署,应用适当的安全权限,除了 DBA/DevOps 之外的任何人都不会看到。
As an addition to the other answers, isn't it better to use the file in Source Control as a template, with just dev/test encrypted connection strings so that it works in dev/test.
For production (or other environments the app is deployed to), the encrypted credentials file is generated separately to the specified template format, managed/updated/deployed separately, has appropriate security permissions applied, never seen by anyone other than DBA/DevOps.