我用RSAProtectedConfigurationProvider加密配置文件里的连接字符串后,在本机正常运行,但一把web.config文件上传到网站虚拟主机里 就提示"……打不开 RSA 密钥容器……" 查了资料知道这种情况是加解密不在同一台电脑上造成的。。大虾们,如何解决????先谢了

解决方案 »

  1.   

    ASP.NET IIS 注册工具 (Aspnet_regiis.exe)官方文档:http://msdn.microsoft.com/zh-cn/library/k6h9cz8h(VS.80).aspx配置文件webform下是web.config,winform下是app.config,如果要加密app.config,先把它改名为:web.config,加密后再重新改回原名就可以了打开SDK命令提示执行以下命令:加密:aspnet_regiis -pef "配置节" "目录"解密:aspnet_regiis -pdf "配置节" "目录"如:aspnet_regiis -pef "connectionStrings" "e:\TestProject"加密e:\TestProject\web.config中设置的connectionStrings节点中的内容读取节点值和未加密前相同,.net会自动解密如果想对web.config的数据库连接字符串进行加密的话,这里提供了两个方法:方法一 
        使用“DataProtectionConfigurationProvider”形式加密,创建test.aspx文件,代码如下: 
    需要添加引用 
    using System.Web.Configuration; 
    using System.IO;//加密 
    protected void Button1_Click(object sender, EventArgs e) 
        { 
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
            ConfigurationSection section = config.GetSection("connectionStrings"); 
            
            if (section != null && !section.SectionInformation.IsProtected) 
            { 
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
                config.Save(); 
            }    } 
    //解密 
        protected void Button2_Click(object sender, EventArgs e) 
        { 
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); 
            ConfigurationSection section = config.GetSection("connectionStrings");        if (section != null && section.SectionInformation.IsProtected) 
            { 
                section.SectionInformation.UnprotectSection(); 
                config.Save(); 
            } 
        } 
    总结:此方法很方便,并且很简单,但安全性没有密钥加密高。方法二 
    使用“RSAProtectedConfigurationProvider”形式来加密 
    test.aspx程序文件基本如上, 
    把section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
    改成section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); 
    但这个时候你访问网站的时候很有可能会出现说明: 在处理向该请求提供服务所需的配置文件时出错。请检查下面的特定错误详细信息并适当地修改配置文件。 
    分析器错误信息: 未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。这样的错误,解决方法是: 
    进dos运行:aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE" 
    如果运行出错,需要把目录 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 放入环境变量path中。 
    此时就可以成功访问网站了。 
    同样可以通过命令行来实现“RSAProtectedConfigurationProvider”加密