C# - 使用aspnet_regiis.exe找不到配置部分错误

时间:2016-07-14 20:20:41

标签: c# encryption app-config aspnet-regiis.exe

我正在尝试加密我的app.config文件中的敏感连接字符串信息,以便我正在开发的C#应用​​程序。我使用以管理员身份运行的VS命令promt中的以下命令:

aspnet_regiis.exe -pef "Credentials" "C:\Users\.....\MyProjectFolderDir"

这是我的app.config文件的结构:

<?xml version="1.0" encoding="utf-8" ?>
<config>
    <configSections>
      <section name="ApplicationSettings" type="sometype"/>
      <section name="WebSettings" type="sometype"/>
      <section name="Credentials" type="sometype"/>
      <section name="SQLServerSettings" type="sometype"/>
    </configSections>

    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>

    <ApplicationSettings   Mode="mymode"
                           FileSearchMode="myfilemode"
                           SubtractHoursDebugging="0"/>

    <WebSettings WebApiServers=""
                    CredentialMethod="mymethod"/>

    <Credentials
                    Domain="mydomain"
                    UserName="myusername"
                    Password="mypassword"/>

    <SQLServerSettings
        ConnectionString="Server=***********"/>

  </config>

但是,我一直收到以下错误:

  

加密配置部分...   找不到配置部分“凭据”。   失败!

如何才能加密我的部分?

3 个答案:

答案 0 :(得分:0)

您的配置文件应以<configuration>元素开头,而不是<config>。由于<config> aspnet_regiis.exeCredentials稀疏为嵌套元素,因此错误。使用当前的配置文件,命令应为

aspnet_regiis.exe -pef "config\Credentials" "C:\Users\.....\MyProjectFolderDir"

答案 1 :(得分:0)

首先,您可以从自定义配置部分How to create custom config section in app.config?获得答案,这是msdn https://msdn.microsoft.com/en-us/library/2tw134k3.aspx

的示例

其次,类型通常是指一个真实的模型,所以你应该输入命名空间和你创建的类来模拟你想要使用的配置类型,如下所示:

 <configuration>
 <configSections>
  <section name="sampleSection"
           type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1" setting2="value two" 
              setting3="third value" />
</configuration>

希望有所帮助

答案 2 :(得分:0)

事实证明,aspnet-regiis.exe专门用于web.config文件。除非您重命名为web.config,否则它不适用于app.config文件。我没有在每次想要加密/解密时重命名我的app.config,而是最终创建了一个类,每次运行应用程序时都会处理它。确保您使用以下内容:

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

班级:

internal class Crypto
{
    internal static AppSettingsSection GetEncryptedAppSettingsSection(string exeConfigName)
    {
        // Open the configuration file and retrieve 
        // the connectionStrings section.
        System.Configuration.Configuration config = ConfigurationManager.
            OpenExeConfiguration(exeConfigName);

        AppSettingsSection section =
                config.GetSection("appSettings")
                as AppSettingsSection;

        EncryptConfigSection(config, section);
        return section;
    }

    internal static ConnectionStringsSection GetEncryptedConnectionStringsSection(string exeConfigName)
    {
        // Open the configuration file and retrieve 
        // the connectionStrings section.
        System.Configuration.Configuration config = ConfigurationManager.
            OpenExeConfiguration(exeConfigName);

        ConnectionStringsSection section =
                config.GetSection("connectionStrings")
                as ConnectionStringsSection;

        EncryptConfigSection(config, section);
        return section;
    }

    internal static void EncryptConfigSection(System.Configuration.Configuration config, ConfigurationSection section)
    {
        //Ensure config sections are always encrypted
        if (!section.SectionInformation.IsProtected)
        {
            // Encrypt the section.
            section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
            // Save the current configuration.
            config.Save();
        }
    }
}