[英]Encrypt a custom section in app/web.config file
我需要加密/解密app.config中的自定义部分以及web.config文件。 我读到aspnet_regiis可用于web.config,但我需要以编程方式执行此操作。
打开mappedExeConfiguration后,我指定一个部分如下:
ConfigurationSection connStrings = config.AppSettings;
加密/解密AppSettings部分。
如何指定自定义部分的名称? 当我在configurationSection对象之后键入我的自定义部分的名称时,智能无法识别它。 (它只识别一些众所周知的部分)
PS在我的函数中,我需要将自定义节名称作为字符串参数。
例:
例如
<Configuration>
<MyCustomTag>
<... data />
</MyCustomTag>
</Configuration>
其中MyCustomTag是我需要加密/解密的部分。
我通过使用http://www.a2zmenu.com/Blogs/CSharp/How-to-encrypt-configuration-file.aspx上的代码实现了这一点。
我会粘贴我的代码,但基本上它与该网页上的代码完全相同,除了更改应用程序名称。
编辑:对于自定义部分,我不确定,因为我不需要使用它,但您可以在以下行中探索配置对象为您提供的内容。
Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");
这是我的整个UpdateKey()方法,我现在意识到我从网页上改编了一下。 也许有帮助。
public static void UpdateKey(string key, string newValue)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(GetAppPath() + "MyAppName.exe");
config.AppSettings.Settings[key].Value = newValue;
config.Save();
}
然后,在我保存了我的钥匙之后,我打电话
EncryptAppSettings("appSettings");
也许你可以调整param值以适应那里。
从VS 2010的CommandPromt调用加密命令:
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis -pef "connectionStrings" "YOUR_PROJECT_NAME" -prov "DataProtectionConfigurationProvider"
解密:
%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis -pdf "connectionStrings" "YOUR_PROJECT_NAME"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.