asp.net下加密Config的方法

來源:互聯網
上載者:User

複製代碼 代碼如下:<appSettings>
<!-- 連接字串是否加密 -->
<add key="ConStringEncrypt" value="false"/>
<!-- 連接字串,(可以擴充支援不同資料庫)如果是加密方式,上面一項要設定為true,如果是明文server=127.0.0.1;database=codematic;uid=sa;pwd=,上面設定為false -->
<add key="ConnectionString" value="Data Source=|DataDirectory|\wm.mdb;Persist Security Info=True"/>
<!--許可權模組連接字串-->
<add key="ConnectionStringAccounts" value="Data Source=|DataDirectory|\wm.mdb;Persist Security Info=True"/>
<add key="ConnectionString2" value="Data Source=|DataDirectory|\wm.mdb;Persist Security Info=True"/>
<!--虛擬目錄名稱(如果是網站,則為空白) -->
<add key="VirtualPath" value=""/>
<!--登入頁地址 -->
<add key="LoginPage" value="admin/Login.aspx"/>
<!--預設菜單是否是展開狀態-->
<add key="MenuExpanded" value="false"/>
<!--實體物件內容緩村的時間(分鐘)-->
<add key="ModelCache" value="30"/>
</appSettings>

在asp.net2.0中新增了對web.config中的部分資料進行加密的功能,可以使用RSAProtectedConfigurationProvider和DPAPIProtectedConfigurationProvider來加密,本文說明使用RSAProtectedConfigurationProvidert和電腦層級的密鑰容器進行加密的步驟。
1. 首先確定要進行加密的web.config中的配置節是否可以加密
2. 建立RSA密鑰容器
3. 在web.config中標識要使用的密鑰容器
4. 對web.config進行加密
5. 授予對 RSA 密鑰容器的存取權限
Step 1:首先確定要進行加密的web.config中的配置節是否可以加密
ASP.NET 2.0支援對Web.config的部分配置節進行加密,以下配置節中的資料是不能進行加密的: 複製代碼 代碼如下:* <processModel>
* <runtime>
* <mscorlib>
* <startup>
* <system.runtime.remoting>
* <configProtectedData>
* <satelliteassemblies>
* <cryptographySettings>
* <cryptoNameMapping>
* <cryptoClasses>

Step2:建立 RSA 密鑰容器
若要建立 RSA 密鑰容器,請使用 ASP.NET IIS 註冊工具 (Aspnet_regiis.exe) 及 –pc 開關。必須為密鑰容器指定一個名稱,該名稱標識應用程式的 Web.config 檔案的 configProtectedData 節中指定的 RsaProtectedConfigurationProvider 所使用的密鑰容器。為確保可以匯出新建立的 RSA 密鑰容器,必須包括 -exp 選項。
例如,下面的命令建立一個名為 ABeenKeys 的 RSA 密鑰容器,該容器是可匯出的電腦級密鑰容器。
aspnet_regiis -pc "ABeenKeys"–exp
Step 3: Modify web.config to identify the key container
編輯Web.config檔案以標識要使用的密鑰容器
在web.config中加以<configProtectedData>來配置密鑰容器, 使用名為 ABeenKeys 的電腦級 RSA 密鑰容器的
在<configuration>中加入xmlns屬性
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
使用名為 ABeenKeys 的電腦級 RSA 密鑰容器的 saProtectedConfigurationProvider。 複製代碼 代碼如下:<configProtectedData >
<providers>
<add name="ABeenProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, processorArchitecture=MSIL"
keyContainerName="ABeenKeys"/>
</providers>
</configProtectedData>

Step 4: Encrypt the <connectionStrings> section of your web.config file
加密你的web.config檔案中的配置節
> aspnet_regiis -pe "connectionStrings" -app "/connectionTest" 
Step 5:授予對 RSA 密鑰容器的存取權限
可以通過以下代碼確定應該給哪個使用者權限
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
預設情況下,RSA 密鑰容器受到所在伺服器上的 NTFS 存取控制清單 (ACL) 的嚴密保護。這樣能夠限制可以訪問加密金鑰的人員,從而增強加密資訊的安全性。必須首先向 ASP.NET 應用程式的進程標識授予對該 RSA 密鑰容器的讀取存取許可權,然後 ASP.NET 才能使用 RSA 密鑰容器。可以使用 Aspnet_regiis.exe 工具及 -pa 開關,向 ASP.NET 應用程式的標識授予讀取 RSA 密鑰容器的許可權。例如,下面的命令向 Windows Server 2003 NETWORK SERVICE 帳戶授予對名為 ABeenKeys 的電腦級 RSA 密鑰容器的讀取存取許可權:
aspnet_regiis -pa "ABeenKeys" "NT AUTHORITY\NETWORK SERVICE"
注意:
如果 RSA 密鑰容器是使用者級容器,必須以其 Windows 設定檔儲存了密鑰的使用者的身份登入,並且必須包括 -pku 選項以授予對該使用者級 RSA 密鑰容器的存取權限。
若要使用電腦配置中指定的預設 RsaProtectedConfigurationProvider,必須首先嚮應用程式的 Windows 標識授予對名為 NetFrameworkConfigurationKey 的電腦密鑰容器的存取權限,該電腦密鑰容器是為該預設提供者指定的密鑰容器。例如,下面的命令向 NETWORK SERVICE 帳戶授予對預設 RsaProtectedConfigurationProvider 所使用的 RSA 密鑰容器的存取權限。
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
NetFrameworkConfigurationKey RSA 密鑰容器是 Aspnet_regiis.exe 工具所發出的命令的預設密鑰容器。因此上述命令也可以按以下方式發出:
aspnet_regiis -pa "NT AUTHORITY\NETWORK SERVICE"

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.