ASP.NET (VB) 如何存取,加解密 Web.Config 的 AppSettings

<%@ Page Language=”VB” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

<script runat=”server”>

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = WebConfigurationManager.AppSettings(TextBox1.Text)
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not Page.IsPostBack Then
            DropDownList1.DataSource = WebConfigurationManager.AppSettings.Keys
            DropDownList1.DataBind()
            TextBox2.Text = WebConfigurationManager.AppSettings(DropDownList1.SelectedValue)
        End If
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        TextBox2.Text = WebConfigurationManager.AppSettings(DropDownList1.SelectedValue)
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ‘1, 寫入Runtimeg設定
        ‘WebConfigurationManager.AppSettings.Set(DropDownList1.SelectedValue, TextBox2.Text)
        ‘2, 修改Web.config
        ‘2-1, 開啟web.config
        Dim cfg As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        ‘2-2, 修改設定
        cfg.AppSettings.Settings(DropDownList1.SelectedValue).Value = TextBox2.Text
        ‘2-3, 存檔
        cfg.Save()
    End Sub

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        ‘1, 開啟web.config
        Dim cfg As System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        ‘2, 取得appSettings設定區段
        Dim cs As ConfigurationSection = cfg.GetSection(“appSettings”)
        ‘3, 判斷是否有加密過, 以決定加密或解密
        If cs.SectionInformation.IsProtected Then
            cs.SectionInformation.UnprotectSection()
            Response.Write(“已解密…”)
        Else
            cs.SectionInformation.ProtectSection(“DataProtectionConfigurationProvider”)
            Response.Write(“已加密…”)
        End If
        ‘4, 存檔
        cfg.Save()
    End Sub
</script>

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
    <title>Untitled Page</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        key :
        <asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>
        <asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” Text=”Get Value” />
        <asp:Label ID=”Label1″ runat=”server” Text=”Label”></asp:Label><br />
        <br />
        <br />
        keys :
        <asp:DropDownList ID=”DropDownList1″ runat=”server” AutoPostBack=”True” OnSelectedIndexChanged=”DropDownList1_SelectedIndexChanged”>
        </asp:DropDownList>
        <asp:TextBox ID=”TextBox2″ runat=”server”></asp:TextBox>
        <asp:Button ID=”Button2″ runat=”server” Text=”修改” OnClick=”Button2_Click” />
        <asp:Button ID=”Button3″ runat=”server” OnClick=”Button3_Click” Text=”加密/解密” /></div>
    </form>
</body>
</html>

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *