加密连接字符串还会在web.config中添加空元素

时间:2017-08-21 06:32:19

标签: c# asp.net .net encryption web-config

为什么这段代码也会在web.config中添加无用的空元素?注意:这是来自Advanced Installer的Custom Action,之前它运行良好。

我为高级安装程序添加的唯一2条额外行是

var map = new ExeConfigurationFileMap { ExeConfigFilename = path }; var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

高级安装程序:加密连接Sting的自定义操作。

[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
    try
    {
        var delimiter = '|';
        var actionData = session["CustomActionData"];
        var dataArray = actionData.Split(delimiter);
        var server = dataArray[0];
        var database = dataArray[1];
        var username = dataArray[2];
        var password = dataArray[3];
        var path = Path.Combine(dataArray[4], "web.config");
        var connStr = BuildConnStr(server, database, username, password);

        var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
        var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        var section = (ConnectionStringsSection)config.GetSection("connectionStrings");

        // Add Connectionstrings element
        section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr));

        // Encrypt
        section.SectionInformation.ProtectSection(ConnStrEncryptionKey);

        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified, true);

        return ActionResult.Success;
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message + "Trace: " + ex.StackTrace, ex.Message);
        throw;
    }
}

web.config,包含所有额外无用的空元素

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <configuration>
    <configProtectedData />
    <system.diagnostics />
    <system.windows.forms />
    <System.Windows.Forms.ApplicationConfigurationSection />
    <uri />
    <appSettings>
      <add key="ClientValidationEnabled" value="true" />
      <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
    <system.web>
      <anonymousIdentification />
      <authentication />
      <authorization />
      <clientTarget />
      <deployment />
      <deviceFilters />
      <fullTrustAssemblies />
      <healthMonitoring />
      <hostingEnvironment />
      <httpCookies />
      <httpHandlers />
      <httpModules />
      <identity />
      <machineKey />
      <membership />
      <mobileControls />
      <pages />
      <partialTrustVisibleAssemblies />
      <processModel />
      <profile />
      <protocols />
      <roleManager />
      <securityPolicy />
      <sessionPageState />
      <sessionState />
      <siteMap />
      <trace />
      <trust level="Full" />
      <urlMappings />
      <webControls clientScriptsLocation="/aspnet_client/{0}/{1}/" />
      <webParts />
      <webServices />
      <xhtmlConformance />
      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" />
      <globalization requestEncoding="utf-8" responseEncoding="utf-8"
        fileEncoding="utf-8" culture="en-US" uiCulture="en-US" />
      <customErrors mode="On" />
      <caching>
        <cache />
        <outputCache />
        <outputCacheSettings />
        <sqlCacheDependency />
      </caching>
    </system.web>
    <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"
            culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
            culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
          <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35"
            culture="neutral" />
          <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
    <system.webServer>
      <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <remove name="OPTIONSVerbHandler" />
        <remove name="TRACEVerbHandler" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*."
          verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      </handlers>
    </system.webServer>
    <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
      <EncryptedData>
        <CipherData>
          <CipherValue>xxx</CipherValue>
        </CipherData>
      </EncryptedData>
    </connectionStrings>
    <system.net>
      <authenticationModules />
      <connectionManagement />
      <defaultProxy />
      <requestCaching />
      <settings />
      <webRequestModules />
      <mailSettings>
        <smtp />
      </mailSettings>
    </system.net>
    <system.runtime.caching>
      <memoryCache />
    </system.runtime.caching>
    <system.runtime.serialization>
      <dataContractSerializer />
    </system.runtime.serialization>
    <system.serviceModel>
      <behaviors />
      <bindings />
      <client />
      <comContracts />
      <diagnostics />
      <extensions />
      <protocolMapping />
      <routing /> 
      <serviceHostingEnvironment />
      <services />
      <standardEndpoints />
      <tracking />
    </system.serviceModel>
    <system.serviceModel.activation>
      <diagnostics />
      <net.pipe />
      <net.tcp />
    </system.serviceModel.activation>
    <system.transactions>
      <defaultSettings />
    </system.transactions>
    <system.web.extensions>
      <scripting>
        <scriptResourceHandler />
        <webServices>
          <authenticationService />
          <jsonSerialization />
          <profileService />
          <roleService />
        </webServices>
      </scripting>
    </system.web.extensions>
    <system.xaml.hosting>
      <httpHandlers />
    </system.xaml.hosting>
    <system.xml.serialization>
      <dateTimeSerialization />
      <schemaImporterExtensions />
      <xmlSerializer />
    </system.xml.serialization>
  </configuration>

解决方案:将ConfigurationSaveMode设置为Minimal

0 个答案:

没有答案