Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Web.config中自动加密连接字符串_C#_Encryption_Web Config - Fatal编程技术网

C# 在Web.config中自动加密连接字符串

C# 在Web.config中自动加密连接字符串,c#,encryption,web-config,C#,Encryption,Web Config,我有下面的类来加密连接字符串 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Configuration; using System.Web.Security; using System.Configuration; namespace App.WebUI.Infrastructure { public static class

我有下面的类来加密连接字符串

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Configuration;
using System.Web.Security;
using System.Configuration;

namespace App.WebUI.Infrastructure
{
    public static class ConnectionStringEncryption
    {
        private static readonly string Path = HttpRuntime.AppDomainAppVirtualPath;

        public static void EncryptConnString()
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Path);
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                config.Save();
            }
        }

        public static void DecryptConnString()
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(Path);
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (section.SectionInformation.IsProtected)
            {
                section.SectionInformation.UnprotectSection();
                config.Save();
            }
        }
    }
}
在Global.asax.cs

        if(!System.Diagnostics.Debugger.IsAttached)
            ConnectionStringEncryption.EncryptConnString();
当我在没有“if(!System.Diagnostics.Debugger.IsAttached)”的情况下尝试使用local时,效果很好,但当我发布时,会出现以下错误。对路径“…\web.config”的访问被拒绝

我无权更改服务器中文件的权限,也不想更改

是否有其他方法可以在不更改权限的情况下完成此操作


由于某种原因,加密web.config文件的过程需要有写入权限。也许您应该问的问题是,如何以具有写入文件权限的用户身份运行该进程。我的答案是,您可以将AppPool更改为使用具有写入文件权限的用户,或者将代码放入另一种可执行文件中,例如,在安装时(作为管理员)而不是在运行时执行


如果由于没有正确的计算机密钥而无法解密加密的web.config文件,则应设计一个进程,在安装过程中而不是在应用程序运行时在目标计算机上加密该文件。您需要询问有关发布过程的更多信息,因为您不应该使用运行应用程序的同一帐户加密文件。您应该有一个在目标计算机上运行的安装进程。

您正在使用此代码写入磁盘。运行网站应用程序池的帐户没有执行此操作的权限。所以不要这样做。有很多方法可以保护此类信息。读取并将其写入磁盘不是一个好主意。最好是问“由于XYZ,我(认为我需要)将加密/受保护的连接字符串写入磁盘。如何从我的网站执行此操作?”每次发布时,在web.config中自动加密连接字符串的最佳方法是什么?这是要求之一。不想使用aspnet\u regiis.exe工具。您不能,除非。。。不,我甚至不想说。授予你的应用程序池标识对网站目录的写入权限是非常糟糕的。无论如何,你为什么要加密它?您不应该发布加密版本并对其进行解密吗?这很奇怪。你不信任你的托管公司吗?如果他们想要你的connex字符串,他们可以得到它——他们拥有你的appdomain。在这个该死的程序上附加一个调试器,他们就知道了。这是公司要求的。。由于密钥问题,我无法加密和发布。。如果我加密并发布,IIS无法解密以获取连接字符串。我正在使用VS2013部署应用程序。我知道WebDeploy3.5有一个功能可以帮我处理这个问题。您知道如何将此规则“–EnableRule:EncryptWebConfig”添加到VS2013部署工具中吗?抱歉,我不太熟悉各种版本的Visual Studio web部署功能。我建议您通过Visual Studio或其他方式搜索或提出一个新问题,更具体地说是关于部署加密的web.config文件。检查