web.cofig中数据库连接字符串加密解密

一、aspnet_regiis.exe 文件路径:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727

 

、利用Dos命令创建一个可导出的rsa密钥容器,命名为Key 
aspnet_regiis -pc "
Key" -exp 

 

三、web.config中配置

<!--对数据库连接字符串进行加密-->

 <configProtectedData>

    <providers>

      <clear />

      <add name="KeyProvider" type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" keyContainerName="Key" useMachineContainer="true"/>

    </providers>

 </configProtectedData>

<connectionStrings>

    <add name="ConnectionStr" connectionString="Data Source=.\sqlexpress;Initial Catalog=MembershipTest;Integrated Security=True"

      providerName="System.Data.SqlClient" />

 </connectionStrings>

注解:Key是第二步已创建好的密钥容器名称

 

注意:以上配置在在configuration节下,并且确保configuration节点下的xmlns属性有如下值:

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

 

四、利用Dos命令对web.config文件进行加密:

aspnet_regiis -pef "connectionStrings" "D:\test\Membership1" -prov "KeyProvider"

注解:

connectionStrings:要加密的web.config节点名称,

D:\test\Membership1web.config文件所在的物理路径(不包含文件名)

KeyProviderweb.config文件中添加的加密节点名称

 

五、利用Dos命令对web.config文件进行解密:

aspnet_regiis -pdf "connectionStrings" " D:\test\Membership1" 

你可能感兴趣的:(数据库连接)