日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Web.config配置文件的加密,解密及读写操作

發(fā)布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Web.config配置文件的加密,解密及读写操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

代碼都比較簡單.不用多做說明:

Code
aspx:
<body>
<form?id="form1"?runat="server">
<div>
<asp:Button?ID="Button1"?runat="server"?Text="加密"?OnClick="Button1_Click"?/>
&nbsp;&nbsp;
<asp:Button?ID="Button2"?runat="server"?OnClick="Button2_Click"?Text="解密"?/><br?/>
<br?/>
<asp:TextBox?ID="txtConnectionStr"?runat="server"?Width="717px"></asp:TextBox><br?/>
<asp:Button?ID="Button3"?runat="server"?OnClick="Button3_Click"?Text="保存鏈接字串"?/>
&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;
&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;?&nbsp;
<asp:Button?ID="Button4"?runat="server"?OnClick="Button4_Click"?Text="測試連線"?/></div>
</form>
</body>

aspx.cs
protected?void?Page_Load(object?sender,?EventArgs?e)
{

}
protected?void?Button1_Click(object?sender,?EventArgs?e)
{
Configuration?config?
=?WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection?section?
=?config.Sections["connectionStrings"];
if?(!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(
"RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave?
=?true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected?void?Button2_Click(object?sender,?EventArgs?e)
{
Configuration?config?
=?WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection?section?
=?config.ConnectionStrings;
if?(section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
section.SectionInformation.ForceSave?
=?true;
config.Save(ConfigurationSaveMode.Modified);
}
}
protected?void?Button4_Click(object?sender,?EventArgs?e)
{
string?str?=?ConfigurationManager.ConnectionStrings["PEP"].ToString();
Response.Write(ConnectionTesting(str));
}
protected?void?Button3_Click(object?sender,?EventArgs?e)
{
//測試數(shù)據(jù)庫鏈接字串是否正確.
if?(ConnectionTesting(txtConnectionStr.Text)?==?"鏈接字串設置成功")
{
//保存當前鏈接字串到config
Configuration?config?=?WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
ConnectionStringsSection?section?
=?(ConnectionStringsSection)config.GetSection("connectionStrings");
section.ConnectionStrings[
"PEP"].ConnectionString?=?txtConnectionStr.Text;
config.Save(ConfigurationSaveMode.Modified);
}
}

protected?string?ConnectionTesting(string?connstr)
{
string?msg?=?"";
string?str?=?string.IsNullOrEmpty(connstr)???""?:?connstr;
SqlConnection?conn?
=?new?SqlConnection(str);
try
{
conn.Open();
msg?
=?"鏈接字串設置成功";
}
catch?(Exception?ex)
{
msg?
=?ex.Message.ToString();
}
finally
{
conn.Close();
}

return?msg;
}

?

轉載于:https://www.cnblogs.com/jimmyblog/archive/2008/08/18/1270570.html

總結

以上是生活随笔為你收集整理的Web.config配置文件的加密,解密及读写操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。