1、打开记事本,然后将下面的代码复制到一个新文件中。
<%@ Page Language="C#" %>
<%
Response.Write(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
%>
保存 my.aspx 到你的web目录 ,运行一下窗体显示 “ NT AUTHORITY/NETWORK SERVICE ”。成功!
2、(关键一步)运行cmd,执行以下
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY/NETWORK SERVICE"
说明:注册默认的 RsaProtectedConfigurationProvider 的RSA 密钥容器,
NetFrameworkConfigurationKey 是 RsaProtectedConfigurationProvider 的默认provider。 成功!
3、现在,可以加密web.config ,运行:
加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb"
说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb"
成功!
4、这样就可以在程序里调用了(不用解密, 哈哈):
...
string connstr= ConfigurationManager.ConnectionStrings["myConnstr"].ConnectionString.ToString();
...
5、查看已解密的配置值
打开文本编辑器,然后将下面的 ASP.NET 代码复制到一个新文件中。
Visual Basic<%@ Page Language="VB" %> <%@ Import Namespace="System.Configuration" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat="server"> Public Sub Page_Load() ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings ConnectionStringsGrid.DataBind() Dim config As System.Configuration.Configuration = _ WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath) Dim key As MachineKeySection = _ CType(config.GetSection("system.web/machineKey"), MachineKeySection) DecryptionKey.Text = key.DecryptionKey ValidationKey.Text = key.ValidationKey End Sub </script> <html> <body> <form runat="server"> <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" /> <P> MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR> MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" /> </form> </body> </html>
C#<%@ Page Language="C#" %> <%@ Import Namespace="System.Configuration" %> <%@ Import Namespace="System.Web.Configuration" %> <script runat="server"> public void Page_Load() { ConnectionStringsGrid.DataSource = ConfigurationManager.ConnectionStrings; ConnectionStringsGrid.DataBind(); Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); MachineKeySection key = (MachineKeySection)config.GetSection("system.web/machineKey"); DecryptionKey.Text = key.DecryptionKey; ValidationKey.Text = key.ValidationKey; } </script> <html> <body> <form runat="server"> <asp:GridView runat="server" CellPadding="4" id="ConnectionStringsGrid" /> <P> MachineKey.DecryptionKey = <asp:Label runat="Server" id="DecryptionKey" /><BR> MachineKey.ValidationKey = <asp:Label runat="Server" id="ValidationKey" /> </form> </body> </html>将该文件另存为 “walkthrough.aspx”,然后在浏览器中查看该文件。
您将看到加密的 Web.config 文件中已解密的值。
若要保持网站上敏感信息的私密性,请在完成此演练时删除 walkthrough.aspx 文件。
同样的,也可以用创建自己的RSA 密钥容器,如下:
(1)、创建 "MyKeys" 密钥容器,运行:aspnet_regiis -pc "MyKeys" -exp
(2)、在web.config里加入以下:
<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)、授予帐户对计算机级别的 "MyKeys" RSA 密钥容器的访问权限,运行:
aspnet_regiis -pa "MyKeys" "NT AUTHORITY/NETWORK SERVICE"
(4)、现在,可以加密web.config ,运行:
加密:aspnet_regiis -pe "connectionStrings" -app "/Myweb" -prov "MyProvider"
说明:"connectionStrings" 是要加密的节,"/Myweb"是的web目录,"MyProvider" 自己密钥容器
解密:aspnet_regiis -pd "connectionStrings" -app "/Myweb" -prov "MyProvider"