我加密了 web.config
使用以下代码文件
protected void Page_Load(object sender, EventArgs e)
{
EncriptSection("connectionStrings", "DataProtectionConfigurationProvider");
}
private void EncriptSection(string sectionName, string provider)
{
Configuration config =
WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.GetSection(sectionName);
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
}
加密 web.config 文件后,站点在本地系统中运行良好,但是当我发布我的站点并部署到服务器时。
页面未读取 web.config
文件并给出错误。当我解密文件然后将网站发布到服务器时,网站在服务器上运行良好。
如何加密文件并将其运行到服务器。
Below is the error which i am getting now :
运行时错误
描述:服务器发生应用程序错误。此应用程序的当前自定义错误设置阻止远程查看应用程序错误的详细信息(出于安全原因)。然而,它可以被运行在本地服务器机器上的浏览器查看。
详细信息:要使此特定错误消息的详细信息能够在远程计算机上查看,请创建一个 <customErrors>
web.config
内的标签配置文件位于当前Web应用程序的根目录下。这<customErrors>
标签应该有它的 mode
属性设置为 Off
.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
注意:您看到的当前错误页面可以通过修改 defaultRedirect
替换为自定义错误页面。应用程序的属性 <customErrors>
配置标记以指向自定义错误页面 URL。
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
</system.web>
</configuration>
****在我输入之后**
<customErrors mode="Off"/>
**在配置中出现以下错误****
Parser Error Message: Failed to decrypt using provider 'DataProtectionConfigurationProvider'. Error message from the provider: Key not valid for use in specified state. (Exception from HRESULT: 0x8009000B)
来源错误:
Line 13: </system.web>
Line 14: <connectionStrings
configProtectionProvider="DataProtectionConfigurationProvider">
Line 15: <EncryptedData>
Line 16: <CipherData>
Line 17: <CipherValue>reertertertreterterterterterterterte
rtertertertertert5345345345 </CipherValue>
源文件:C:\FOLDER\web.config 行:15
最佳答案
您需要使用解密的 web.config 部分发布您的应用程序,因为用于加密/解密的 key 是特定于机器的。
要在线加密 web.config 部分,请调用 global.asax
的 Application_Start()
中的 ProtectSection()
方法。
因此 ProtectSection()
方法将在应用程序第一次启动时执行此操作。
关于c# - 发布后 Web.Config 文件加密不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27834283/