加密(Asp.Net配置文件的)配置节
生活随笔
收集整理的這篇文章主要介紹了
加密(Asp.Net配置文件的)配置节
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
序言
開發(fā)者經(jīng)常需要在配置文件中存儲一些配置信息。比如,數(shù)據(jù)庫聯(lián)接字符串,用戶名和密碼等。(這就提出)一個要求,使用ASP.NET 1.x的時候,.net framework應(yīng)該提供一些方法密或解密這些信息。慶幸的是,ASP.NET 2.0可以靈活的滿足這一要求。這篇文章將會展示怎樣用嵌入的工具和代碼保護(hù)一個特定的配置節(jié)。
怎樣加密配置文件?
ASP.NET 2.0允許你加密配置文件的一部分,來確保他們的安全(相對的)。這個特征通常被叫做受保護(hù)配置(Protected Configuration)。ASP.NET提供了兩種方法來加密和解密配置文件
在命令提示行中,通過ASPNET_REGIIS.EXE工具?
通過 configuration management 類?
加密與解密通過兩個受保護(hù)的提供者實現(xiàn)
RsaProtectedConfigurationProvider?
DataProtectionConfigurationProvider?
存儲在可以在你的機(jī)器里的machine.config文件中找到這兩個提供者。RsaProtectedConfigurationProvider是默認(rèn)的提供者。下面的部分是machine.config中的相關(guān)標(biāo)記
<configProtectedData
defaultProvider="RsaProtectedConfigurationProvider">
<providers>
<add name="RsaProtectedConfigurationProvider"
type="System.Configuration.RsaProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
description="Uses RsaCryptoServiceProvider to encrypt and decrypt"
keyContainerName="NetFrameworkConfigurationKey"
cspProviderName="" useMachineContainer="true" useOAEP="false" />
<add name="DataProtectionConfigurationProvider"
type="System.Configuration.DpapiProtectedConfigurationProvider,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"
description="Uses CryptProtectData and CryptUnProtectData
Windows APIs to encrypt and decrypt" useMachineProtection="true"
keyEntropy="" />
</providers>
</configProtectedData>
使用ASPNET_REGIIS.EXE加密一個配置節(jié)
讓我們來看看如何使用ASPNET_REGIIS.EXE工具來加密<connectionStrings>配置節(jié)。
用VS.Net建立一個網(wǎng)站叫做EncryptTest,加入一個web.config文件。然后加入如下的<connectionStrings>配置節(jié):
<connectionStrings>
<add name="connstr" connectionString=
"data source=.\sqlexpress;initial catalog=
northwind;integrated security=true" />
</connectionStrings>
我們簡單的加入了一個聯(lián)接字符串,指向了Northwind數(shù)據(jù)庫。然后打開default.aspx文件,拖拽一個GridView控件,并在Page_Load中輸入如下代碼:
protected void Page_Load(object sender, EventArgs e)
{
string strConn = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from customers", strConn);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}?
這段代碼里,我們用ConfigurationManager類從<connectionStrings>配置節(jié)讀取了聯(lián)接字符串。這里用們有意的用了手動的方法代替了SqlDataSource空間,就是為了證明內(nèi)置的類ConfigurationManager自動的機(jī)密了加密版本的聯(lián)機(jī)字符串。
?現(xiàn)在打開Visual Studio 2005命令提示,執(zhí)行下面的命令:
aspnet_regiis -pe "connectionStrings" -app "/encrypttest"
-pe開關(guān)用來指定web.config中需要加密的節(jié)(我們的例子里是connectionStrings)。
-app開關(guān)用來指定IIS里面的虛擬路徑
執(zhí)行以后,如果你再查看一下web.config文件,你會看到這樣的情形:
<connectionStrings configProtectionProvider=
"RsaProtectedConfigurationProvider">
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm=
"http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm=
"http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
<KeyName>Rsa Key</KeyName>
</KeyInfo>
<CipherData>
<CipherValue>NW4gFUtlA3XkbKu42FQ3kYV8EKmwzy9r53vrI2rjV
ZFqjsr00/MwS6TWqjnsguN09kWvNIrbfWu5+Gi+DLFhYnGm2NcuaCy
Vic8f5e0Q8u3E7zk2MegZmiri5bSELE1fZneWz4oFb+MHqA94ZO3Be
XBlocou6ydtmJPXZCqLsDQ=</CipherValue>
</CipherData>
</EncryptedKey>
</KeyInfo>
<CipherData>
<CipherValue>T2fswq6Rt7g7VSNUnoe4kNFGSluWCBReQf3DRbXao/
sWaWs4mrJAI6Xy0zNDHY5pKXUUF9Kep2wG84rMVx0QtLIUjBaUKCnrm
Eb+53oYNPjN4Kf5zcPyWoaWwcus4LnJYNtg3oGJUvavZ0tjGXH9+5gB
w/xMrtfDcYAIom9l/IMcO92BKrvimjn/k4Mr8VXxGpvdMkAC3+e6dtW
JeUgQGpepO+RNpWymB5kWj38LjMQ=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>
這個工具已經(jīng)用RsaProtectedConfigurationProvider(默認(rèn)的提供者)加密了connectionStrings節(jié)點,并且將加密過的標(biāo)簽返回給配置文件。你還可以指定具體的Protected Configuration Provider(加密保護(hù)配置提供者),使用-prov開關(guān)來指定。很簡單,不是么?
現(xiàn)在,瀏覽你的web頁面。這個web頁面將會在GridView標(biāo)記里正確的顯示Customers表的內(nèi)容,這說明ConfigurationManager類在讀取連接字符串的時候自動解密了其中的信息。
使用ASPNET_REGIIS.EXE解密配置節(jié)
如果你需要在開發(fā)過程中對連接串進(jìn)行些改動要怎么辦?不用擔(dān)心,ASPNET_REGIIS.EXE工具同樣支持解密節(jié)點,僅僅需要執(zhí)行下面的命令就會恢復(fù)到你原來的未加密的版本。aspnet_regiis -pd "connectionStrings" -app "/encrypttest"唯一的區(qū)別就是,我們用-pd開關(guān)代替了-pa開關(guān)。
Encryption and decrypting via code
The task of encrypting and decrypting web.config can also be achieved via code. Let's see how.
Add a new web form to the same web site and design it as shown below:
通過代碼加密與解密 加密與解密web.config功能同樣可以通過代碼來完成。讓我們看看怎樣完成的。在同樣的站點下加入一個新的web頁面,并且設(shè)計成下面的樣子
在"Encrypt Connection String"按鈕的Click事件中加入如下代碼:
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.ConnectionStrings;
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection
("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
?
如果你不是很熟需怎樣修改web.config的內(nèi)容,下面的這篇文章應(yīng)該對你有幫助
通過程序修改Web.config文件的內(nèi)容。
這段代碼的上半部分是打開并修改web.config文件。然后找到ConnectionStrings配置節(jié)。然后,粗體字的部分是非常重要的。ProtectSection()方法指定了你用來加密節(jié)點的Protected Configuration Provider(保護(hù)配置供應(yīng)者),然后用這個供應(yīng)者加密了下一行的節(jié)點。代碼最后將結(jié)果保存。
解密配置節(jié)的工作和加密類似:
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);
}
}
這里我們用UnprotectSection()方法回復(fù)到未加密的版本。
概述The encryption and decryption can be done in two ways - using ASPNET_REGIIS command line tool or programmatically.ASP.NET 2.0讓保護(hù)你的web.config文件變得非常容易,它允許你通過加密的方式進(jìn)行保護(hù)。無論是加密還是解密都有兩種辦法--使用ASPNET_REGIIS命令行工具 或 程序控制。
總結(jié)
以上是生活随笔為你收集整理的加密(Asp.Net配置文件的)配置节的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在网页上启动你的应用程序
- 下一篇: 有一种进步叫放弃