C# - 使用 aspnet_regiis.exe 找不到配置节错误
C# - The configuration section was not found error using aspnet_regiis.exe
我正在尝试为我正在开发的 C# 应用程序加密 app.config 文件中的敏感连接字符串信息。我以管理员身份使用 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>
但是,我不断收到以下错误:
Encrypting configuration section...
The configuration section 'Credentials' was not found.
Failed!
我怎样才能得到这个来加密我的部分?
您的配置文件应以 <configuration>
元素而不是 <config>
开头。因为它是 <config>
aspnet_regiis.exe
细化 Credentials
作为嵌套元素,因此错误。使用您当前的配置文件,命令应该是
aspnet_regiis.exe -pef "config\Credentials" "C:\Users\.....\MyProjectFolderDir"
首先,这里有一个关于自定义配置部分的答案,您可以从中学习 How to create custom config section in app.config? and here is an example from msdn https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
其次,该类型通常指的是真实模型,因此您应该输入命名空间和您创建的 class 来模拟您要使用的配置类型,如下所示:
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1" setting2="value two"
setting3="third value" />
</configuration>
希望对您有所帮助
事实证明,aspnet-regiis.exe 专用于 web.config 文件。它不适用于 app.config 文件,除非您重命名为 web.config。我没有每次都想 encrypt/decrypt 重命名我的 app.config,而是创建了一个 class 来在我每次 运行 应用程序时处理这个问题。确保您使用的是以下内容:
using System.Configuration;
using System.Web.Security;
Class:
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();
}
}
}
我正在尝试为我正在开发的 C# 应用程序加密 app.config 文件中的敏感连接字符串信息。我以管理员身份使用 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>
但是,我不断收到以下错误:
Encrypting configuration section... The configuration section 'Credentials' was not found. Failed!
我怎样才能得到这个来加密我的部分?
您的配置文件应以 <configuration>
元素而不是 <config>
开头。因为它是 <config>
aspnet_regiis.exe
细化 Credentials
作为嵌套元素,因此错误。使用您当前的配置文件,命令应该是
aspnet_regiis.exe -pef "config\Credentials" "C:\Users\.....\MyProjectFolderDir"
首先,这里有一个关于自定义配置部分的答案,您可以从中学习 How to create custom config section in app.config? and here is an example from msdn https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
其次,该类型通常指的是真实模型,因此您应该输入命名空间和您创建的 class 来模拟您要使用的配置类型,如下所示:
<configuration>
<configSections>
<section name="sampleSection"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>
<sampleSection setting1="Value1" setting2="value two"
setting3="third value" />
</configuration>
希望对您有所帮助
事实证明,aspnet-regiis.exe 专用于 web.config 文件。它不适用于 app.config 文件,除非您重命名为 web.config。我没有每次都想 encrypt/decrypt 重命名我的 app.config,而是创建了一个 class 来在我每次 运行 应用程序时处理这个问题。确保您使用的是以下内容:
using System.Configuration;
using System.Web.Security;
Class:
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();
}
}
}