c# - 加密连接字符串 - 允许其他机器解密
问题描述
我正在尝试开发一个连接到 SQL 数据库的 C# Winform 应用程序。
我的配置文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="myConnNameString"
providerName="System.Data.SqlClient"
connectionString="Data Source=myServerName;Initial
Catalog=myDefDatabase;User=myUser;Password=myPassword;Application Name=myAppName" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
我能够通过以下方式成功加密此文件:
aspnet_regiis.exe -pef "connectionStrings" path_to_config_file
但是一旦我这样做了,我就不能再正常运行我的应用程序了。现在我只能通过以管理员身份运行它来做到这一点
Cannot decrypt with provider RsaProtectedConfigurationProvider.
Cannot open key container RSA
现在据我了解: https://docs.microsoft.com/en-us/previous-versions/msp-np/ff650304(v=pandp.10)
我创建了一个带有机器级容器的 RSA,所以只有加密文件的机器可以解密它,没有其他机器可以解密。
我怎样才能让其他机器也解密这个文件?我的应用程序存储在远程网络驱动器上的单个目录中,每个人都可以访问该目录。该应用程序通过快捷方式在他们的计算机上运行。
先感谢您!
编辑:还值得一提的是,并非我们公司的每个人都有本地管理员权限。
解决方案
好的,这就是我到目前为止所完成的:
创建可导出的 RSA 机器级密钥容器
aspnet_regiis -pc "MyKeys" -exp
现在我授予 NT AUTHORITY\NETWORK SERVICE 帐户对该 conatiner 的读取访问权限(无论它是什么)
aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"
然后我用我的自定义提供程序加密了我的配置文件
aspnet_regiis -pef "connectionStrings" "path_to_config_file" -
prov "myCustomProvider"
配置文件如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configProtectedData>
<providers>
<add name="myCustomProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a,
processorArchitecture=MSIL"
keyContainerName="MyKeys"
useMachineContainer="true" />
</providers>
</configProtectedData>
<configSections>
</configSections>
<connectionStrings>
<add name="myConnectionString"
providerName="System.Data.SqlClient"
connectionString="Data Source=myServerName;Initial
Catalog=myDefDatabase;User=myUser;Password=myPassword;Application
Name=myAppName" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
然后我将我的 RSA 密钥容器导出到一个文件:
-px "MyKeys" "C:\Users\name.surname\Desktop\test\test.xml" -pri
我现在应该在哪里导入这个 xml 文件?我的程序的每个用户都必须执行这一行吗?
aspnet_regiis -pi "MyKeys" test.xml
现在,当我尝试运行我的 Winform 程序(甚至在执行加密的机器上)时,我收到以下错误消息:
无法使用提供程序 myCustomProvider 解密。提供者错误消息。解码 OAEP 完成时出错
任何帮助将不胜感激。
推荐阅读
- java - 如何在 Android 中切换主机
- ruby-on-rails - 无法在 Rspec 中发送带有标头的 put 请求
- ios - IOS/Objective-C:在没有 Storyboard Segue 的情况下可以在模态转换中使用自定义 Segue?
- android - 无法比较微调器选择的字符串值
- latex - 在背面将编译器更改为 XeLeTax 但仍然失败
- reporting-services - SSRS 多列报表分页不正确
- java - 在java中从JSON中删除评论的最快方法?
- multithreading - 运行单独线程的结果不清楚
- scala - 如何在 Slick 中设置获取大小?
- python - 如何使用xpath和scrapy使用href从该站点提取URL?