【发布时间】:2011-10-26 14:40:47
【问题描述】:
我一直在阅读有关 C#/ASP 应用程序的 web.config 某些部分的加密和解密的信息,并且我成功地为我的应用程序加密了 web.config 的连接字符串。我的问题是解密。我正在使用标准代码进行加密和解密,但它修改了 web.config。在本地它工作正常,因为当它修改 web.config 时我可以保存它并且它仍然会运行,但是当我将它上传到远程服务器时它就不起作用了。
我得到的错误是
配置错误说明:在安装过程中发生错误 处理服务此请求所需的配置文件。 请查看下面的具体错误详细信息并修改您的 适当的配置文件。
解析器错误消息:无法使用提供程序解密 'RsaProtectedConfigurationProvider'。来自提供商的错误消息: 不良数据
加密
try
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
ConfigurationSection section = config.GetSection("connectionStrings");
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
config.Save();
}
catch (Exception ex)
{
}
解密
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection("connectionStrings");
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
config.Save();
}
每当页面加载时我都会调用解密方法,但它不起作用,它给了我上面的错误。
我根本无法访问主机服务器。所以使用命令行不是一个选项。
【问题讨论】:
-
只是好奇,为什么您需要手动解密应用中的连接字符串?只需访问
WebConfigurationManager.ConnectionStrings即可为您解密配置文件(如果已加密)。 -
这只是交给我的设计。我现在正在修改代码来做到这一点。我将连接字符串从后面的 C# 代码分配给 ASP SQLDataSource。
-
我尝试在后面的代码中分配它,但仍然出现错误。我在 page_load 期间为其中一个 asp 页面放置了代码以进行测试。
-
字符串 cString = WebConfigurationManager.ConnectionStrings["NameOfConnectionString"].ConnectionString; NameOfSqlDataSource.ConnectionString = cString;
-
您是否尝试过在 .aspx 页面中以声明方式分配连接字符串?类似
<asp:SqlDataSource ConnectionString="NameOfConnectionString"
标签: c# asp.net .net connection-string