App.config 连接字符串保护错误
-
09-06-2019 - |
题
我遇到了以前遇到的问题;找不到我关于如何解决它的参考。
这就是问题所在。我们使用以下代码对客户端应用程序的 app.config 中的连接字符串部分进行加密:
config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
If config.ConnectionStrings.SectionInformation.IsProtected = False Then
config.ConnectionStrings.SectionInformation.ProtectSection(Nothing)
' We must save the changes to the configuration file.'
config.Save(ConfigurationSaveMode.Modified, True)
End If
问题是我们有一名销售人员休假。旧笔记本电脑将交给新销售人员,并在新用户登录下,当它尝试执行此操作时,我们会收到错误。错误是:
Unhandled Exception: System.Configuration.ConfigurationErrorsException:
An error occurred executing the configuration section handler for connectionStrings. ---> System.Configuration.ConfigurationErrorsException: Failed to encrypt the section 'connectionStrings' using provider 'RsaProtectedConfigurationProvider'.
Error message from the provider: Object already exists.
---> System.Security.Cryptography.CryptographicException: Object already exists
解决方案 2
我在我最初对自己的回答中找到了一个更优雅的解决方案。我发现如果我只是以最初安装应用程序的用户身份登录并导致配置文件连接字符串被加密,然后在命令提示符下转到 .net Framework 目录并运行
aspnet_regiis -pa "NetFrameworkConfigurationKey" "{domain}\{user}"
它授予其他用户访问 RSA 加密密钥容器的权限,然后它适用于其他用户。
只是想在这里添加它,因为我以为我已经在我们的开发博客上发布了这个问题,但在这里找到了它,所以如果我需要再次查找它,它会在这里。也将在此线程中添加到我们的开发博客点的链接。
其他提示
http://blogs.msdn.com/mosharaf/archive/2005/11/17/protectedConfiguration.aspx#1657603
复制并粘贴:D
2007 年 2 月 12 日星期一 12:15 AM 作者:Naica
关于:使用受保护的配置加密配置文件
以下是我在 PC 上加密两个部分然后将其部署到 Web 服务器所执行的所有步骤的列表。也许它会对某人有所帮助......:
创建机器级 RSA 密钥容器
aspnet_regiis -pc "DataProtectionConfigurationProviderKeys" -exp
将其添加到 web.config 中的 connectionStrings 部分之前:
<add name="DataProtectionConfigurationProvider" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" keyContainerName="DataProtectionConfigurationProviderKeys" useMachineContainer="true" />
不要错过
<clear />
从上面!多次加密/解密时很重要检查以确保其位于 Web.Config 文件的顶部。如果缺少请添加:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
在VS中保存并关闭Web.Config文件(非常重要!)
在命令提示符(我的本地电脑)窗口中,转到:
C:\WINNT\Microsoft.NET\Framework\v2.0.50727
加密:(请注意更改应用程序的物理路径,或使用 -app 选项并为应用程序提供虚拟目录的名称!因为我在 PC 上使用 VS,所以我更喜欢下面的选项。该路径是Web.config文件的路径)
aspnet_regiis -pef“connectionStrings”“c:\Bla\Bla\Bla”-prov“DataProtectionConfigurationProvider”
aspnet_regiis -pef“system.web/membership”“c:\Bla\Bla\Bla”-prov“DataProtectionConfigurationProvider”
解密(如果需要的话!):
aspnet_regiis -pdf "connectionStrings" "c:\Bla\Bla\Bla" aspnet_regiis -pdf "system.web/membership" "c:\Bla\Bla\Bla"
删除密钥容器(仅在需要时!)
aspnet_regiis -pz "DataProtectionConfigurationProviderKeys"
将以上密钥保存到 xml 文件,以便将其从本地 PC 导出到 Web 服务器(UAT 或 Production)
aspnet_regiis -px "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml -pri
在 WebServer 服务器上导入密钥容器:
aspnet_regiis -pi "DataProtectionConfigurationProviderKeys" \temp\mykeyfile.xml
授予对 Web 服务器上密钥的访问权限
aspnet_regiis -pa "DataProtectionConfigurationProviderKeys" "DOMAIN\User"
在 IIS 中查看 ASP.NET 用户或使用:
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name
删除授予对 Web 服务器上密钥的访问权限(仅在需要时!)
aspnet_regiis -pr "DataProtectionConfigurationProviderKeys" "Domain\User"
将加密的 Web.config 文件复制并粘贴到 WebServer。
所以我确实让它工作了。
- 从笔记本电脑中删除旧用户帐户
- 重置 app.config 以使部分不受保护
- 从所有用户机器密钥中删除了密钥文件
- 运行应用程序并允许它保护该部分
但这所做的一切只是让它为该用户工作。
现在我需要知道我必须做什么来更改代码以保护该部分,以便 PC 上的多个用户可以使用该应用程序。虚拟 PC 我来了(明天到下周三 WDW 假期结束后)!
有什么建议可以帮助我指明正确的方向,因为我对 RSA 加密类型的东西不是很有经验。
听起来像是权限问题。有问题的(新)用户是否具有 app.config 文件的写入权限?先前的用户是否是本地管理员或高级用户,可以掩盖此问题?