Update Configuration : WebConfigurationManager « Configuration « ASP.NET Tutorial






<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="UpdateConfig" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Update Configuration</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:Button ID="Button1" runat="server" Text="Stop access to .XYZ Resources" OnClick="Button1_Click" />&nbsp;
            <br />
            <asp:Button ID="Button2" runat="server" Text="Re-enable access to .XYZ Resources" OnClick="Button2_Click" />&nbsp;
            <br />
            <asp:Button ID="Button3" runat="server" Text="Encrypt connectionStrings" OnClick="Button3_Click" />&nbsp;
            <br />
            <asp:Button ID="Button5" runat="server" Text="Read Connection String" OnClick="Button5_Click" />&nbsp;
            <br />
            <asp:Button ID="Button4" runat="server" Text="Decrypt connectionStrings" OnClick="Button4_Click" />&nbsp;
    </div>
    </form>
</body>
</html>

File: Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;


public partial class UpdateConfig : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = @"system.web/httpHandlers";
        string path = "/";

        Configuration appConfig = WebConfigurationManager.OpenWebConfiguration(path);
        HttpHandlersSection section = (HttpHandlersSection)appConfig.GetSection(name);


        HttpHandlerAction newHandler = new HttpHandlerAction(
            "*.xyz", "System.Web.HttpForbiddenHandler", "*");
        section.Handlers.Add(newHandler);
        appConfig.Save();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string name = @"system.web/httpHandlers";
        string path = "/";

        Configuration appConfig = WebConfigurationManager.OpenWebConfiguration(path);
        HttpHandlersSection section = (HttpHandlersSection)appConfig.GetSection(name);

        section.Handlers.Remove("*", "*.xyz");
        appConfig.Save();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string name = "connectionStrings";
        string path = "/";
        Configuration cfg = WebConfigurationManager.OpenWebConfiguration(path);
        ConnectionStringsSection section;
        section = (ConnectionStringsSection) cfg.GetSection(name);
        section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
        cfg.Save();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string name = "connectionStrings";
        string path = "/";
        Configuration cfg = WebConfigurationManager.OpenWebConfiguration(path);
        ConnectionStringsSection section;
        section = (ConnectionStringsSection)cfg.GetSection(name);
        section.SectionInformation.UnprotectSection();
        cfg.Save();
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Write(WebConfigurationManager.ConnectionStrings["LocalNWind"].ConnectionString);
    }
}








16.32.WebConfigurationManager
16.32.1.Using the Configuration API
16.32.2.Get and set application settings
16.32.3.Read configuration
16.32.4.Retrieving a configuration setting with the GetSection() and GetWebApplicationSection() methods.
16.32.5.Opening a Configuration File
16.32.6.Update Configuration