asp.net 2.0 中加密web.config 文件中的配置节
点击次数:15 次 发布日期:2008-11-26 11:32:20 作者:源代码网
|
源代码网推荐 源代码网推荐 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 AUTHORITYNETWORK SERVICE" 源代码网推荐 注意: 源代码网推荐 源代码网推荐 如果 RSA 密钥容器是用户级容器,必须以其 Windows 配置文件存储了密钥的用户的身份登录,并且必须包括 -pku 选项以授予对该用户级 RSA 密钥容器的访问权限。 源代码网推荐 源代码网推荐 源代码网推荐 若要使用计算机配置中指定的默认 RsaProtectedConfigurationProvider,必须首先向应用程序的 Windows 标识授予对名为 NetFrameworkConfigurationKey 的计算机密钥容器的访问权限,该计算机密钥容器是为该默认提供程序指定的密钥容器。例如,下面的命令向 NETWORK SERVICE 帐户授予对默认 RsaProtectedConfigurationProvider 所使用的 RSA 密钥容器的访问权限。 源代码网推荐 源代码网推荐 aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITYNETWORK SERVICE" 源代码网推荐 NetFrameworkConfigurationKey RSA 密钥容器是 Aspnet_regiis.exe 工具所发出的命令的默认密钥容器。因此上述命令也可以按以下方式发出: 源代码网推荐 源代码网推荐 aspnet_regiis -pa "NT AUTHORITYNETWORK SERVICE" 源代码网推荐 源代码网推荐 代码下载http://www.cnblogs.com/Files/abeen/connectionTest.rar 源代码网推荐 注意:我发现这个方法有个缺陷,哪就是在每次加密完后,重新启动机算机发现IIS admin出错了,还得重新安装 iis 郁闷! 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
