2016-07-14 61 views
1

我想加密我的app.config文件中的C#應用​​程序,我正在開發敏感的連接字符串信息。我使用下面的命令從VS命令PROMT以管理員身份運行:C# - 找不到配置部分錯誤使用aspnet_regiis.exe

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!

我怎樣才能得到這個加密我科?

+0

您是否嘗試過使用 「配置/證書」?您可能需要深入研究。 – Ju66ernaut

+0

你的app.config文件不應該以開始而不是?因此,該文件的開始應該是<?xml version =「1.0」encoding =「utf-8」?> Mangist

+0

從技術上說是的,但是我在模式方面遇到問題, ''配置的效果被聲明兩次''所以我只是將它重命名爲配置。 –

回答

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(); 
     } 
    } 
} 
0

您的配置文件應該以<configuration>元素開頭,而不是<config>。因爲它是<config>aspnet_regiis.exe變薄Credentials作爲嵌套元素,從而出現錯誤。根據您當前的配置文件中的命令應該是

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

我試過這個,但它仍然不起作用。有人可以嘗試在他們的機器上? –

+0

C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727> aspnet_regiis.exe -pef「config/Credentials」「C:\ Users \ Me \ Desktop」 加密配置部分... 配置節'config /憑證'未找到。 失敗! –

+0

我也嘗試將配置更改爲配置 –

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> 

希望它可以幫助

相關問題