首页 文章

加密Web.config并安装

提问于
浏览
2

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

我已经多次按照_1466148中的步骤进行操作了 .

请注意关于演练,我的web.config文件中没有任何machineKeys,所以我跳过了加密步骤 .

当我跑到aspnet_regiis -pef connectionStrings "c:\Users......\mywebsite.com"
返回是:加密配置部分......成功!

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

'/'应用程序中的

服务器错误 .

配置错误说明:处理为此请求提供服务所需的配置文件时发生错误 . 请查看下面的具体错误详细信息并相应地修改配置文件 .

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

来源错误:

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

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


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


我知道必须有一些遗失,但我已经搜索过,但没有找到任何东西 . 我通过电子邮件向托管公司询问他们是否需要对加密网站做任何事情,但他们还没有回复 .

我所期望的是,有一个密钥驻留在其他地方,它使用加密值并使用algorhythm对其进行解密 . 如果是这样的话,我会在哪里获得该密钥以及它将去哪里 .

非常感谢任何帮助,有点惊讶我在网上找不到任何类似的问题 .

非常感谢 .

2 回答

  • 2

    Jonny O - 谢谢 . 这很容易 . CP

    我添加了global.asax文件,这里是进入此文件的代码片段(global.asax.cs) .

    其中大部分内容都是从上面复制的,但这是我的整个解决方案 . 再次感谢 .

    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();
        }
    
  • 3

    我没有直接回答你的问题,但这是一个加密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 .

相关问题