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
I am running into an issue I had before; can't find my reference on how to solve it.
Here is the issue. We encrypt the connection strings section in the app.config for our client application using code below:
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
The issue is we had a salesperson leave. The old laptop is going to a new salesperson and under the new user's login, when it tries to to do this we get an error. The error is:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来像是权限问题。 有问题的(新)用户是否具有 app.config 文件的写入权限? 先前的用户是否是本地管理员或高级用户,可以掩盖此问题?
Sounds like a permissions issue. The (new) user in question has write permissions to the app.config file? Was the previous user a local admin or power user that could have masked this problem?
所以我确实让它工作了。
但这所做的只是让它为该用户工作。
现在我需要知道我必须做什么来更改代码以保护该部分,以便 PC 上的多个用户可以使用该应用程序。 虚拟 PC 我来了(明天到下周三 WDW 假期结束后)!
有什么建议可以帮助我指明正确的方向,因为我对 RSA 加密类型的东西不是很有经验。
So I did get it working.
But all this did was get it working for this user.
NOW I need to know what I have to do to change the code to protect the section so that multiple users on a PC can use the application. Virtual PC here I come (well after vacation to WDW tomorrow through next Wednesday)!
any advice to help pointing me in right direction, as I am not very experienced in this RSA encryption type stuff.
我在我最初对自己的回答中找到了一个更优雅的解决方案。 我发现如果我只是以最初安装应用程序的用户身份登录并导致配置文件连接字符串被加密并在命令提示符下转到 .net Framework 目录并运行
它,则会授予其他用户访问 RSA 加密密钥的权限容器,然后它可以为其他用户工作。
只是想在此处添加它,因为我以为我已经在我们的开发博客上发布了这个问题,但在这里找到了它,所以如果我需要再次查找它,它会在这里。 也将在此线程中添加到我们的开发博客点的链接。
I found a more elegant solution that in my original answer to myself. I found if I just logged in as th euser who orignally installed the application and caused the config file connectionstrings to be encrypted and go to the .net framework directory in a commadn prompt and run
it gave the other user permission to access the RSA encryption key container and it then works for the other user(s).
Just wanted to add it here as I thought I had blogged this issue on our dev blog but found it here, so in case I need to look it up again it will be here. Will add link to our dev blog point at this thread as well.
http://blogs.msdn.com/mosharaf/ archive/2005/11/17/protectedConfiguration.aspx#1657603
复制并粘贴 :D
2007 年 2 月 12 日星期一上午 12:15 由 Naica
回复:使用受保护的配置加密配置文件
此处是我在 PC 上加密两个部分然后将其部署到 Web 服务器所执行的所有步骤的列表。 也许它会对某人有所帮助...:
创建机器级 RSA 密钥容器
将其添加到 web.config 中的 connectionStrings 部分之前:
不要错过上面的
! 多次加密/解密时很重要检查将此内容放在 Web.Config 文件的顶部。 如果缺少,请添加:
在 VS 中保存并关闭 Web.Config 文件(非常重要!)
在命令提示符中(我的本地 PC) ) 窗口转到:
<块引用>
C:\WINNT\Microsoft.NET\Framework\v2.0.50727
加密:(请注意更改应用程序的物理路径,或使用 -app 选项并为应用程序提供虚拟目录的名称!因为我在我的电脑我更喜欢下面的选项。路径是 Web.config 文件的路径)
aspnet_regiis -pef“connectionStrings”“c:\Bla\Bla\Bla”-prov“DataProtectionConfigurationProvider”
aspnet_regiis -pef "system.web/membership" "c:\Bla\Bla\Bla" -prov "DataProtectionConfigurationProvider"
解密(仅在需要时!):
删除密钥容器(仅在需要时!)
将上述密钥保存到 xml 文件,以便将其从本地 PC 导出到 Web 服务器(UAT 或生产)
在 WebServer 服务器上导入密钥容器:
授予对 Web 服务器上密钥的访问权限
在 IIS 中查看 ASP.NET 用户或使用:
删除授予对 Web 服务器上密钥的访问权限(仅在需要时!)
将加密的 Web.config 文件复制并粘贴到 WebServer。
http://blogs.msdn.com/mosharaf/archive/2005/11/17/protectedConfiguration.aspx#1657603
copy and paste :D
Monday, February 12, 2007 12:15 AM by Naica
re: Encrypting configuration files using protected configuration
Here is a list of all steps I've done to encrypt two sections on my PC and then deploy it to the WebServer. Maybe it will help someone...:
To create a machine-level RSA key container
Add this to web.config before connectionStrings section:
Do not miss the
<clear />
from above! Important when playing with encrypting/decrypting many timesCheck to have this at the top of Web.Config file. If missing add it:
Save and close Web.Config file in VS (very important!)
In Command Prompt (my local PC) window go to:
Encrypt: (Be aware to Change physical path for your App, or use -app option and give the name o virtual directory for app! Because I used VS on my PC I preferred the bellow option. The path is the path to Web.config file)
aspnet_regiis -pef "connectionStrings" "c:\Bla\Bla\Bla" -prov "DataProtectionConfigurationProvider"
aspnet_regiis -pef "system.web/membership" "c:\Bla\Bla\Bla" -prov "DataProtectionConfigurationProvider"
To Decrypt (if needed only!):
Delete Keys Container (if needed only!)
Save the above key to xml file in order to export it from your local PC to the WebServer (UAT or Production)
Import the key container on WebServer servers:
Grant access to the key on the web server
See in IIS the ASP.NET user or use:
Remove Grant access to the key on the web server (Only if required!)
Copy and Paste to WebServer the encrypted Web.config file.