简体   繁体   English

加密app / web.config文件中的自定义部分

[英]Encrypt a custom section in app/web.config file

I need to encrypt/decrypt custom sections in app.config as well as web.config file. 我需要加密/解密app.config中的自定义部分以及web.config文件。 I read that aspnet_regiis can be used for web.config, but i need to do this programatically. 我读到aspnet_regiis可用于web.config,但我需要以编程方式执行此操作。

After opening the mappedExeConfiguration, i specify a section as follows: 打开mappedExeConfiguration后,我指定一个部分如下:

ConfigurationSection connStrings = config.AppSettings;

to encrypt/decrypt the AppSettings section. 加密/解密AppSettings部分。

How do i specify the name of the custom section? 如何指定自定义部分的名称? When i type the name of my custom section after the configurationSection object, intelli-sense does not recognize it. 当我在configurationSection对象之后键入我的自定义部分的名称时,智能无法识别它。 (It only recognizes a few well known sections) (它只识别一些众所周知的部分)

PS In my function, i need to take the custom section name as a string parameter. PS在我的函数中,我需要将自定义节名称作为字符串参数。

Example: 例:

eg 例如

<Configuration>
   <MyCustomTag> 
       <... data /> 
   </MyCustomTag> 
 </Configuration>

where MyCustomTag is the section i need to encrypt/decrypt. 其中MyCustomTag是我需要加密/解密的部分。

I achieved this by using code I found at http://www.a2zmenu.com/Blogs/CSharp/How-to-encrypt-configuration-file.aspx 我通过使用http://www.a2zmenu.com/Blogs/CSharp/How-to-encrypt-configuration-file.aspx上的代码实现了这一点

I'd paste my code in, but basically it is pretty much identical to the code on that web page, except for changing the application names. 我会粘贴我的代码,但基本上它与该网页上的代码完全相同,除了更改应用程序名称。

Edit: for a custom section, I'm not sure as I did not need to use it, but you could explore what the config object gives you in the following line. 编辑:对于自定义部分,我不确定,因为我不需要使用它,但您可以在以下行中探索配置对象为您提供的内容。

Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");

Here is my entire UpdateKey() method, which I now realise I adapted a bit from the web page. 这是我的整个UpdateKey()方法,我现在意识到我从网页上改编了一下。 Maybe it helps. 也许有帮助。

public static void UpdateKey(string key, string newValue) 
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");
    config.AppSettings.Settings[key].Value = newValue;
    config.Save();
} 

Then after I have saved my key(s), I call 然后,在我保存了我的钥匙之后,我打电话

EncryptAppSettings("appSettings");

and perhaps you can adapt the param value to suit there too. 也许你可以调整param值以适应那里。

From CommandPromt of VS 2010 call a command for encrypt: 从VS 2010的CommandPromt调用加密命令:

%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis  -pef "connectionStrings" "YOUR_PROJECT_NAME" -prov "DataProtectionConfigurationProvider"

Decrypt: 解密:

%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis  -pdf "connectionStrings" "YOUR_PROJECT_NAME"

To encrypt, use the command line posted by HaGever in is answer. 要加密,请使用HaGever发布的命令行作为答案。

This question has some example code to read app.config files from code. 这个问题有一些示例代码从代码中读取app.config文件。 The code wan't working because the encryption key was not installed on the machine used to decrypt app.config. 代码不能正常工作,因为加密密钥没有安装在用于解密app.config的机器上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM