1、打開記事本,然后將下面的代碼復(fù)制到一個(gè)新文件中。
復(fù)制代碼 代碼如下:
%@ Page Language="C#" %>
%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>
保存 my.aspx 到你的web目錄 ,運(yùn)行一下窗體顯示 “ NT AUTHORITY\NETWORK SERVICE ”。成功!
2、(關(guān)鍵一步)運(yùn)行cmd,執(zhí)行以下
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
說明:注冊默認(rèn)的 RsaProtectedConfigurationProvider 的RSA 密鑰容器,
NetFrameworkConfigurationKey 是 RsaProtectedConfigurationProvider 的默認(rèn)provider。 成功!
3、現(xiàn)在,可以加密web.config ,運(yùn)行:
加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb"
說明:"connectionStrings" 是要加密的節(jié),"/Myweb"是的web目錄
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb"
成功!
4、這樣就可以在程序里調(diào)用了(不用解密, 哈哈
):
...
string connstr= ConfigurationManager.ConnectionStrings["myConnstr"].ConnectionString.ToString();
...
同樣的,也可以用創(chuàng)建自己的RSA 密鑰容器,如下:
(1)、創(chuàng)建 "MyKeys" 密鑰容器,運(yùn)行:aspnet_regiis -pc "MyKeys" -exp
(2)、在web.config里加入以下:
復(fù)制代碼 代碼如下:
protectedData>
providers>
add name="MyProvider"
type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0. 0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d0a3a,
processorArchitecture=MSIL"
keyContainerName="MyKeys"
useMachineContainer="true" />
/providers>
/protectedData>
保存。
(3)、授予帳戶對計(jì)算機(jī)級別的 "MyKeys" RSA 密鑰容器的訪問權(quán)限,運(yùn)行:
aspnet_regiis -pa "MyKeys" "NT AUTHORITY\NETWORK SERVICE"
(4)、現(xiàn)在,可以加密web.config ,運(yùn)行:
加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb"-prov "MyProvider"
說明:"connectionStrings" 是要加密的節(jié),"/Myweb"是的web目錄,"MyProvider" 自己密鑰容器
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb"-prov "MyProvider"