ASP.NET 2.0給我們帶來不少安全性方面的改進,讓我們的Web應用越來越安全。在ASP.NET項目中我們的資料庫連結字串都大多寫在了Web.config的檔案中,資料庫使用者密碼一覽無餘,這樣顯然是不安全的。我們可能自己動手寫了一些加密解密演算法來加密,現在好了在ASP.NET 2.0中系統為我們提供了這樣的功能。
<connectionStrings>
<add name="Pubs" connectionString="Server=localhost;Integrated Security=True;Database=Pubs"
providerName="System.Data.SqlClient" />
<add name="Northwind" connectionString="Server=localhost;Integrated Security=True;Database=Northwind"
providerName="System.Data.SqlClient" />
</connectionStrings>
我們如果有操作Web Server的許可權,我們可以不用Coding來加密,直接使用命令列
aspnet_regiis -pe "connectionStrings" -app "/SampleApplication"
如果我們沒法操作Web Server的話只好採用代碼方法
Configuration config = Configuration.GetWebConfiguration(Request.ApplicationPath);
ConfigurationSection section = config.Sections["connectionStrings"];
section.ProtectSection ("DataProtectionConfigurationProvider");
config.Update();
加密後我們看到
<connectionStrings>
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMndjHoAw...</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>