void UpdateUI()
...{
String path = Request.CurrentExecutionFilePath;
path = path.Substring(0, path.LastIndexOf('/'));
// Get configuration.
Configuration config = WebConfigurationManager.OpenWebConfiguration(path);
// Show XML for app settings.
ConfigurationSection appSettings = config.GetSection("appSettings");
// Set protect button appropriately.
if (appSettings.SectionInformation.IsProtected)
...{
Encrypted.Text = "Yes";
ProtectButton.Text = "Unprotect";
}
else
...{
Encrypted.Text = "No";
ProtectButton.Text = "Protect";
}
// Show XML for app settings.
AppSettingsXml.Text = " " + Server.HtmlEncode(appSettings.SectionInformation.GetRawXml());
// Load XML directly from config file, to show encrypted XML.
String configPath = Server.MapPath("web.config");
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(configPath);
XmlNode appSettingsXml = doc.SelectSingleNode("configuration/appSettings");
AppSettingsEncrypted.Text = " " + Server.HtmlEncode(appSettingsXml.OuterXml);
}
</script>
<html>
<head>
<title>Encrypted Configuration Sections</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Encrypted:<asp:Label runat="server" id="Encrypted" /></h2><asp:Button runat="server" id="ProtectButton" OnClick="ProtectButton_OnClick" />
<h2>Current XML (decrypted):</h2>
<pre>
<asp:Label runat="server" ID="AppSettingsXml" />
</pre>
<h2>Encrypted contents:</h2>
<pre>
<asp:Label runat="server" ID="AppSettingsEncrypted" />
</pre>
</div>
</form>
</body>
</html>
对应配置文件如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configProtectedData />
<appSettings>
<add key="currencyService" value="http://www.microsoft.com/services/currencyService.asmx" />
<add key="creditCardValidationService" value="http://www.microsoft.com/services/cc.asmx" />
</appSettings>
</configuration>