C# - The configuration section was not found error using aspnet_regiis.exe
我正在尝试加密app.config文件中的敏感连接字符串信息,以获得我正在开发的C应用程序。我正在使用以管理员身份运行的vs命令promt中的以下命令:
这是app.config文件的结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?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> |
但是,我一直得到以下错误:
0我怎样才能让这个加密我的分区?
因此,事实证明,aspnet-regiis.exe仅用于web.config文件。除非重命名为web.config,否则它不适用于app.config文件。每次我想要加密/解密时,我都不必重命名app.config,而是创建了一个类,每次运行应用程序时都会处理这个类。请确保使用以下内容:
2班级:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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(); } } } |
首先,这里有一个答案,您可以从自定义配置部分了解如何在app.config中创建自定义配置部分?下面是来自msdn的一个例子https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
第二,类型通常指的是一个真实的模型,因此您应该输入您创建的名称空间和类,以便对您想要使用的配置类型进行建模,如下所示:
1 2 3 4 5 6 7 8 | <configuration> <configSections> <section name="sampleSection" type="System.Configuration.SingleTagSectionHandler" /> </configSections> <sampleSection setting1="Value1" setting2="value two" setting3="third value" /> </configuration> |
希望它有帮助
配置文件应该以
1 | aspnet_regiis.exe -pef"config\Credentials""C:\Users\.....\MyProjectFolderDir" |