Code
1 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
2
3 ConnectionStringsSection connectSection = (ConnectionStringsSection)config.ConnectionStrings;
4
5 if (connectSection != null)
6 {
7 if (connectSection.SectionInformation.IsProtected)
8 {
9 connectSection.SectionInformation.UnprotectSection();
10 }
11 else
12 {
13 connectSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
14
15 }
16
17 config.Save();
18 }
1 Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
2
3 ConnectionStringsSection connectSection = (ConnectionStringsSection)config.ConnectionStrings;
4
5 if (connectSection != null)
6 {
7 if (connectSection.SectionInformation.IsProtected)
8 {
9 connectSection.SectionInformation.UnprotectSection();
10 }
11 else
12 {
13 connectSection.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
14
15 }
16
17 config.Save();
18 }
效果:
<
connectionStrings
configProtectionProvider
="DataProtectionConfigurationProvider"
>
< EncryptedData >
< CipherData >
< CipherValue > AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAgr/ZdPSj8kevK6GPy+eZjAQAAAACAAAAAAADZgAAqAAAABAAAACPdgYTQO/Np/evcJQ4ea2HAAAAAASAAACgAAAAEAAAAEVkao1B0SOkkKfEbNOU/2qAAQAAimiEEyRQ/EvvsFgrqvMS9+I/5cd0Fp07rnj0exglwdBN/Uf2nOZw+QStCI79c+VIad6bYr0hoN+eEYjlt9CIRb8za+Dxqpn7M31Cq/Px0vfcNk7SAzz9Wx/95RIqk8ORwpM6sKikUJGBuh8GwhS+uG+X3xc3019ZgnUJ/Vbd8fg0f+A3P1+lxKe+OtvfvW92LII61Lc86Tge4K2R1+gDcVbadGWrJjEY/X3e9KUiWDahmZ4ouJTIg5TBx7eBPLxmxboOPK7f0j9wIE6exHew1C+U1jHEsDsmxhVi2meD13VPs18vycn67/0H/EUbL5EBpeIE5aWpJJG4iH7knAKd1M4+MrWLD2WTAFfSjo4+l7BcIVq5PV1mbGxeV9d4eRtQMuujN+8HiVZbJLuzeW6xy20M6ZW4G0mgmW3VhCCDCHDp4g7tSAM3oWh4J42B5Np+KjWWv7+Ju+OOHuP1YNsJcG/Qr1KEtjW5uipHfP8ldzS4DBvHSM2IoAAd+m5W9D50FAAAAKa3A9Nrq5v+xdQEB8LItNRDUv3I </ CipherValue >
</ CipherData >
</ EncryptedData >
</ connectionStrings >
< EncryptedData >
< CipherData >
< CipherValue > AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAgr/ZdPSj8kevK6GPy+eZjAQAAAACAAAAAAADZgAAqAAAABAAAACPdgYTQO/Np/evcJQ4ea2HAAAAAASAAACgAAAAEAAAAEVkao1B0SOkkKfEbNOU/2qAAQAAimiEEyRQ/EvvsFgrqvMS9+I/5cd0Fp07rnj0exglwdBN/Uf2nOZw+QStCI79c+VIad6bYr0hoN+eEYjlt9CIRb8za+Dxqpn7M31Cq/Px0vfcNk7SAzz9Wx/95RIqk8ORwpM6sKikUJGBuh8GwhS+uG+X3xc3019ZgnUJ/Vbd8fg0f+A3P1+lxKe+OtvfvW92LII61Lc86Tge4K2R1+gDcVbadGWrJjEY/X3e9KUiWDahmZ4ouJTIg5TBx7eBPLxmxboOPK7f0j9wIE6exHew1C+U1jHEsDsmxhVi2meD13VPs18vycn67/0H/EUbL5EBpeIE5aWpJJG4iH7knAKd1M4+MrWLD2WTAFfSjo4+l7BcIVq5PV1mbGxeV9d4eRtQMuujN+8HiVZbJLuzeW6xy20M6ZW4G0mgmW3VhCCDCHDp4g7tSAM3oWh4J42B5Np+KjWWv7+Ju+OOHuP1YNsJcG/Qr1KEtjW5uipHfP8ldzS4DBvHSM2IoAAd+m5W9D50FAAAAKa3A9Nrq5v+xdQEB8LItNRDUv3I </ CipherValue >
</ CipherData >
</ EncryptedData >
</ connectionStrings >
从页面获取该加密节点时。会自动解密,返回普通文本。
还可以用命令行加密码
aspnet_regiis -pe "要加密的节点名" -app "Web应用程序的虚拟路径" -prov "加密提供程序的名称"
并不是所有的设置都可以被加密的,有些运行在ASP。NET之外的,例如<httpRuntime>节点,通常是由IIS Web服务使用的。