【问题标题】:Encrypting connection string - allowing other machines to decrypt加密连接字符串 - 允许其他机器解密
【发布时间】:2020-06-25 17:09:20
【问题描述】:

我正在尝试开发一个连接到 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-n-p/ff650304(v=pandp.10)

我创建了一个带有机器级容器的 RSA,所以只有加密文件的机器可以 解密它,没有其他机器可以做到。

我怎样才能让其他机器也解密这个文件? 我的应用程序存储在远程网络驱动器上的单个目录中,每个人都可以访问该目录。 该应用程序通过快捷方式在他们的计算机上运行。

提前谢谢你!

编辑: 另外值得一提的是,我们公司并不是每个人都有本地管理员权限。

【问题讨论】:

  • 不是直接的答案,但另一种方法是在配置中存储单个用户名/密码的凭据,而是指定 A/D 身份验证 ( 'Trusted_Connection=True;'),然后在数据库中设置权限供A/D用户/组访问。
  • 我们确实考虑过这种可能性,但是我们在访问数据库上的对象时遇到了一些严重的问题,因此我们决定创建一个具有高级权限的单个 sql 用户。

标签: c# sql-server winforms connection-string aspnet-regiis.exe


【解决方案1】:

好的,这就是我到目前为止所取得的成就:

创建可导出的 RSA 机器级密钥容器

aspnet_regiis -pc "MyKeys" -exp

现在我正在向 NT AUTHORITY\NETWORK SERVICE 帐户授予对该容器的读取访问权限(无论它是什么)

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 完成时出错

任何帮助将不胜感激。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多