【问题标题】:Encrypting Web.config and installing加密 Web.config 并安装
【发布时间】:2012-02-23 03:53:07
【问题描述】:

我是加密过程的新手,曾尝试将加密的 web.config 文件安装到托管公司的服务器上,但未成功。我正在使用 Microsoft Visual Web Developer 2010 Express。

我已多次按照Walkthrough: Encrypting Configuration Information Using Protected 中的步骤进行操作。

请注意关于演练,我的 web.config 文件中没有任何 machineKey,因此我跳过了加密步骤。

当我运行 aspnet_regiis -pef connectionStrings "c:\Users......\mywebsite.com"
回报是: 加密配置部分... 成功了!

2)然后我 FTP 我的 web.config 文件,站点收到以下错误:注意:第 8 行突出显示)

“/”应用程序中的服务器错误。

配置错误 说明:处理服务此请求所需的配置文件期间发生错误。请查看下面的具体错误详细信息并适当地修改您的配置文件。

解析器错误消息:无法使用提供程序“RsaProtectedConfigurationProvider”解密。来自提供商的错误消息:错误数据。

来源错误:

第 6 行: 第 7 行: 第 8 行: 第 10 行:

源文件:C:\HostingSpaces*username**mywebsite.com*\wwwroot\web.config 行:8


版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.1


我知道肯定少了一些东西,但我已经搜索过,但没有找到任何东西。我给托管公司发了电子邮件,询问他们是否需要对网站进行加密,但他们还没有回复。

我期望的是有一个密钥存在于其他地方,它获取加密值并使用算法对其进行解密。如果是这样,我会从哪里得到那个钥匙,它会去哪里。

非常感谢任何帮助,但我在网上找不到任何类似的问题,对此我感到有些惊讶。

非常感谢。

【问题讨论】:

    标签: asp.net web-config password-encryption


    【解决方案1】:

    我没有直接回答您的问题,但这里有一个加密 web.config 的简单技术。这可能不是最好的方法,但它可能足以让你开始。此技术在应用程序启动期间加密 web.config。

    非常重要:确保此代码仅在生产环境中运行。如果您在开发过程中运行它,您将加密您的源 web.config,您将无法取回它。

       private static void EncryptConfig() {
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
    
            foreach (string sectionName in new[] { "connectionStrings", "appSettings" }) {
                ConfigurationSection section = config.GetSection(sectionName);
                if (!section.SectionInformation.IsProtected) {
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                }
            }
    
            config.Save();
        }
    

    然后您可以在 Application_Start() 中调用此方法

    protected void Application_Start() {
                if (IsProduction) {
                    EncryptConfig();
                }
    }
    

    此解决方案并不完美,因为当您将 web.config 部署到生产服务器时,它不会被加密。因为加密是在运行时发生的,所以只有在您的应用程序启动后才会加密。当第一个请求进来时,web.config 将被加密。当第二个请求进来时,您的应用程序将需要重新启动,因为 asp.net 会检测到 web.config 已更改。然后从那时起,您的应用程序将使用加密的 web.config 正常运行。这种技术的好处是加密会自动发生。每当您部署新的 web.config 文件时,它都会在启动期间自动加密。

    重要提示:确保 EncryptConfig() 仅在生产环境中运行,以免加密源 web.config。

    【讨论】:

    • 非常感谢 JohnnyO。我最初试图做的是放入我的 Global.aspx 文件,但后来我意识到你不能把命令放在那里。因此,我随后查找了 Global.aspx.cs 文件,但没有找到。然后我为 Global.aspx.cs 创建了一个类,它进入了我的 App_Code 目录。然后在尝试调试时,我无法进入它。因此,我的下一个尝试是创建一个空白的 Web 表单 EncryptMe.aspx(和 .cs)文件并将此逻辑插入... Presto,web.config 已加密。谢谢你把我送到这个方向。可能有更好的方法,但这有效!
    • 您应该能够在“解决方案资源管理器”中右键单击您的 Global.asax 文件并选择“查看代码”。这应该会调出 Global.asax.cs C# 文件。在那里,您可以创建 Application_Start() 方法。如果你把逻辑放在那里,你可以确定 asp.net 会自动执行你的代码。祝你好运。
    【解决方案2】:

    Jonny O - 谢谢。这很容易奏效。 CP

    我添加了 global.asax 文件,这里是进入该文件 (global.asax.cs) 的代码 sn-ps。

    当然,上面的大部分内容都是重复的,但这是我的全部解决方案。再次感谢。

    using System.Web.Configuration;
    using System.Configuration;
    using System.Web.Hosting;
    
        protected void Application_Start(object sender, EventArgs e)
        {
            //Test to see if this app is being started on the development machine (e.g. in the debugger)
            //This code will encript web.config the first time this program runs.
            //Therefore, it is important to have a backup copy of the non-encrypted web.config as this
            //code below will encrypt it, which is what we want to happen on the production server.            
            if (! System.Diagnostics.Debugger.IsAttached )
            {
                EncryptConfig();  //See below
            }
        }
    
    
    
        /// <summary>
        /// This technique of encrypting the web.config file was learned from this forum post:
        /// http://stackoverflow.com/questions/5602630/encrypting-web-config-and-installing
        /// </summary>
        private static void EncryptConfig()
        {
            System.Configuration.Configuration config = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath);
    
            foreach (string sectionName in new[] { "connectionStrings", "appSettings" })
            {
                ConfigurationSection section = config.GetSection(sectionName);
                if (!section.SectionInformation.IsProtected)
                {
                    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                }
            }
    
            config.Save();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 2016-12-30
      • 2012-08-31
      相关资源
      最近更新 更多