加密并部署app.config

And*_*ter 20 .net c# encryption connection-string

我阅读并测试了很多,以找到加密和部署app.config到不同机器的最佳实践.通常,我希望保护来自第三方的连接字符串的内容,并将应用程序部署到不同的计算机上.我不会手动配置每台机器.

我知道有几种方式:

  • Aspnet_Regiis(RSAProtectedConfigurationProvider,DPAPIProtectedConfigurationProvider)绑定到计算机,用户或自定义.RSA加密密钥.

  • System.Security.Cryptography.ProtectedData 绑定到机器或用户.

  • app.config在第一次执行时加密.哪个不安全.

您建议什么或app.config通过设置或复制和粘贴来加密和向不同机器提供应用程序的最佳做法是什么?

Fab*_*bio 11

步骤1 创建RSA密钥对

aspnet_regiis -pc yourkey -exp
Run Code Online (Sandbox Code Playgroud)

Step2导出XML文件中的密钥

aspnet_regiis -px yourkey keyfile.xml -pri
Run Code Online (Sandbox Code Playgroud)

对于每台机器

Step3导入容器

aspnet_regiis -pi yourkey keyfile.xml (see step 2)
Run Code Online (Sandbox Code Playgroud)

对于每台机器

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" />
Run Code Online (Sandbox Code Playgroud)

然后你可以在一台机器上加密并粘贴到其他机器中,记住必须为用户提供权限

aspnet_regiis -pa yourkey [DOMAIN\USER]
Run Code Online (Sandbox Code Playgroud)

管理员组已获得授权.

欲了解更多信息,请访问http://msdn.microsoft.com/en-us/library/yxw286t2(v=vs.90).aspx

当然,您可以将这些步骤放入PowerShell /批处理文件中

另一种通过代码加密connectionStrings部分的方法是

 var connectionStrings = ConfigurationManager.GetSection("connectionStrings") 
 if(!section.SectionInformation.IsProtected)
     connectionStrings.SectionInformation.ProtectSection("YourProvider");
Run Code Online (Sandbox Code Playgroud)

在连接和客户端/服务器方案中,我建议您在广泛的网络中采用的解决方案是不在app.config中分发连接字符串,而是要求在可以是Web服务或RESTful服务的服务上使用连接的信息用户认证后.

因此,步骤越多越少

  1. 验证用户
  2. 服务时需要连接信息,用户名作为参数(HTTPS协议)
  3. 服务返回连接字符串
  4. 应用它在DB连接

使用此解决方案,如果您有区域服务器或更多服务器,则可以选择用户连接的服务器

我希望能有所帮助