asp.net下加密Config的方法

复制代码 代码如下:

  <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"