App.config连接字符串保护错误
我遇到了我以前遇到的一个问题;找不到我如何解决它的参考。
这是问题所在。我们使用以下代码对客户端应用程序的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
解决方案
回答
听起来像是权限问题。有问题的(新)用户对app.config文件具有写权限?以前的用户是本来可以掩盖此问题的本地管理员还是超级用户?
回答
所以我的确做到了。
- 从笔记本电脑中删除了旧用户帐户
- 重置app.config使其部分不受保护
- 从所有用户机器密钥中删除密钥文件
- 运行应用程序并允许其保护该部分
但是,所有要做的就是让它可以为该用户使用。
现在,我需要知道我该怎么做才能更改代码以保护该部分,以便PC上的多个用户可以使用该应用程序。我来了这里的虚拟PC(明天假期到下个星期三放假之后去WDW)!
任何建议都可以帮助我指出正确的方向,因为我对RSA加密类型的知识还不是很丰富。
回答
在最初对自己的回答中,我找到了一个更优雅的解决方案。我发现我是否以最初安装该应用程序的用户身份登录,并导致对配置文件连接字符串进行加密,并在命令提示符下转到.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日,星期一,内卡(Naica)
回复:使用受保护的配置加密配置文件
这是我加密PC上的两个部分并将其部署到WebServer所完成的所有步骤的列表。也许它某人...:
- 创建计算机级RSA密钥容器
aspnet_regiis -pc "DataProtectionConfigurationProviderKeys" -exp
- 在connectionStrings部分之前将其添加到web.config中:
<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文件(非常重要!)
- 在命令提示符(我的本地PC)窗口中,转到:
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导出到WebServer(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。