第 1 步创建 RSA 密钥对
aspnet_regiis -pc yourkey -exp
Step2 导出你输入的 XML 文件
aspnet_regiis -px yourkey keyfile.xml -pri
每台机器
第三步导入你的容器
aspnet_regiis -pi yourkey keyfile.xml (see step 2)
每台机器
Step4 编辑 machine.config(规范路径 C:\Windows\Microsoft.NET\Framework[64|32]\v[Version]\Config)
在 configProtectedData 部分中添加此元素并设置 defaultProvider="YourProvider"
<add name="YourProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
description="Uses RsaCryptoServiceProvider to encrypt and decrypt for my infrastucture"
keyContainerName="yourkey"
cspProviderName=""
useMachineContainer="true"
useOAEP="false" />
然后你可以在一台机器上加密并粘贴到其他机器上,记住必须授予用户权限
aspnet_regiis -pa yourkey [DOMAIN\USER]
管理员组已获得授权。
欲了解更多信息http://msdn.microsoft.com/en-us/library/yxw286t2(v=vs.90).aspx
当然,您可以将这些步骤放入 powershell/批处理文件中
另一种通过代码加密连接字符串部分的方法是
var connectionStrings = ConfigurationManager.GetSection("connectionStrings")
if(!section.SectionInformation.IsProtected)
connectionStrings.SectionInformation.ProtectSection("YourProvider");
在连接和客户端/服务器场景中,我建议您在广泛的网络中采用的解决方案不是在 app.config 中分发连接字符串,而是在可以是 Web 服务或用户认证后的 RESTful 服务。
因此,更多或更少
- 验证用户身份
- 需要在服务中使用用户名作为参数的连接信息(HTTPS 协议)
- 服务返回连接字符串
- 在 DB 上连接的应用程序
使用此解决方案,如果您有区域服务器或更多服务器,您可以选择用户连接的服务器
希望对你有帮助