The example will Encrypt / Decrypt web config file for 3 section which is ConnectionString, AppSetting, and system.web/authentication .
Front End aspx Code - Encrypt
1: <asp:Button ID="Button1" runat="server" Text="Encrypt Web config"
2: OnClick="Button1_Click" />
Code Behind
1: private static string[] sectionName = { "connectionStrings", "appSettings", "system.web/authentication" };
2: public static string[] SectionName
3: {
4: get
5: {
6: return sectionName;
7: }
8: set
9: {
10: sectionName = value;
11: }
12: }
13: protected void Button1_Click(object sender, EventArgs e)
14: {
15: //encrypt
16: ProtectSection();
17: }
18: /// <summary>
19: /// Encrypte WebConfig file for certain section
20: /// </summary>
21: /// <param name="sectionName"></param>
22: /// <param name="provider"></param>
23: private void ProtectSection()
24: {
25: string provider = "DataProtectionConfigurationProvider";
26: foreach (string a in SectionName)
27: {
28: Configuration config =
29: WebConfigurationManager.
30: OpenWebConfiguration("~/");
31: ConfigurationSection section =
32: config.GetSection(a);
33: if (section != null &&
34: !section.SectionInformation.IsProtected)
35: {
36: section.SectionInformation.ProtectSection(provider);
37: config.Save();
38: }
39: }
40: }
Output
Front End aspx code - Decrypt
1: <asp:Button ID="Button2" runat="server"
2: Text="Decrypt Web Config" OnClick="Button2_Click" />
Code behind
1: private static string[] sectionName = { "connectionStrings", "appSettings", "system.web/authentication" };
2: public static string[] SectionName
3: {
4: get
5: {
6: return sectionName;
7: }
8: set
9: {
10: sectionName = value;
11: }
12: }
13: protected void Button2_Click(object sender, EventArgs e)
14: {
15: //decrypt
16: UnProtectSection();
17: }
18: /// <summary>
19: /// Decrypted web config file for certain section
20: /// </summary>
21: /// <param name="sectionName"></param>
22: private void UnProtectSection()
23: {
24: foreach (string a in SectionName)
25: {
26: Configuration config =
27: WebConfigurationManager.
28: OpenWebConfiguration("~/");
29: ConfigurationSection section =
30: config.GetSection(a);
31: if (section != null &&
32: section.SectionInformation.IsProtected)
33: {
34: section.SectionInformation.UnprotectSection();
35: config.Save();
36: }
37: }
38: }
Output
Check if Web.Config is Encrypt or not
The example will check web config file for 3 section which is ConnectionString, AppSetting, and system.web/authentication .Code Behind
1: private static string[] sectionName = { "connectionStrings", "appSettings", "system.web/authentication" };
2: public static string[] SectionName
3: {
4: get
5: {
6: return sectionName;
7: }
8: set
9: {
10: sectionName = value;
11: }
12: }
13: /// <br />
14: <summary>
15: /// method to check if web config file is encrypted or not
16: /// </summary>
17: /// <returns></returns>
18: private bool CheckWebConfigIfEncrypt()
19: {
20: bool isEncrypt = false;
21: foreach (string a in SectionName)
22: {
23: Configuration config =
24: WebConfigurationManager.
25: OpenWebConfiguration("~" + HttpContext.Current.Request.ApplicationPath);
26: ConfigurationSection section =
27: config.GetSection(a);
28: if (section != null &&
29: section.SectionInformation.IsProtected)
30: isEncrypt = true;
31: else
32: {
33: return false;
34: }
35: }
36: return isEncrypt;
37: }
0 comments:
Post a Comment