我的网站的web.config中 存着数据库连接字符串
<appSettings>
<add key="strCnn" value="server=maoning;uid=sa;pwd=jksdha;database=newzcpdm"/>
<add key="strCnnNetcheck" value="server=maoning;uid=sa;pwd=hjhsd;database=netcheck"/>
<add key="limit" value="70"/>
</appSettings>
现在网站要发布到别的机器上,不想让人直接看见这些信息。怎么样加密这一段信息,而不改网站程序中的读取连接字符串的相关代码呢。
在网上查了相关知识,用regiis 什么命令行的。 这些操作是在开发的机器上做还是在即将发布的机器上做呢。都没太看明白。
哪位大哥能说的明白些。谢谢了
<appSettings>
<add key="strCnn" value="server=maoning;uid=sa;pwd=jksdha;database=newzcpdm"/>
<add key="strCnnNetcheck" value="server=maoning;uid=sa;pwd=hjhsd;database=netcheck"/>
<add key="limit" value="70"/>
</appSettings>
现在网站要发布到别的机器上,不想让人直接看见这些信息。怎么样加密这一段信息,而不改网站程序中的读取连接字符串的相关代码呢。
在网上查了相关知识,用regiis 什么命令行的。 这些操作是在开发的机器上做还是在即将发布的机器上做呢。都没太看明白。
哪位大哥能说的明白些。谢谢了
public void Encryption()
{
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
ConfigurationSection section = config.AppSettings;
if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Modified);
}
}
通常我们都会把数据库连接字符串也在web.config中,网站不是在服务器上,为了防止其他用户获取服务器数据库连接密码,需要
对web.config中的字符串加密。
解决方法:
1:通常做法是通过加密、解密算法,通过自己的密钥把连接字符串加密后,在放入web.config中,在程序dbhelper中获取连接字符串时候
再进行解密。
2:利用.net2.0自身的加密工具加密。
步骤:
1.添加密钥
执行:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pc "leador" -exp
其中"leador"为密钥名称2.添加web.config节点
在web.config的<configuration></configuration>之间添加:
<configProtectedData>
<providers>
<add keyContainerName="leador" useMachineContainer="true" description="Uses RsaCryptoServiceProvider to encrypt and
decrypt" name="leador" type="System.Configuration.RsaProtectedConfigurationProvider,System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</configProtectedData>
注意:这里keyContainerName="leador"和name="leador"分别表示你的密钥名称;3.加密web.config
到网站根目录添加一个批处理文件enweb.bat,内容如下:
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "E:\HS Studio\Donet2\Hnlawyer" -prov "leador"
//这里是加密数据库连接字符串 (connectionStrings)
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" "E:\HS Studio\Donet2\Hnlawyer" -prov "leador"
PAUSE
注册上面的的路径和名称!
运行后出现成功!
4.解密
同样到网站根目录添加一个批处理文件deweb.bat,内容如下:
@echo off
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "E:\HS Studio\Donet2\Hnlawyer"
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pdf "connectionStrings" "E:\HS Studio\Donet2\Hnlawyer"
PAUSE最后就是要注意:做完后找到C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys目录下,找到生成
的密钥文件(可按时间查找),给上network service可读取权限,不然会出现Error message from the provider: The RSA key container could
not be opened. 无法读取,这样可能会出现,如果没有自己的服务器,没有权限修改MachineKeys目录这样就可以简便的保护你的数据库。另外:
ASP.NET 2.0支持对Web.config的部分配置节进行加密,以下配置节中的数据是不能进行加密的:
<processModel>
<runtime>
<mscorlib>
<startup>
<system.runtime.remoting>
<configProtectedData>
<satelliteassemblies>
<cryptographySettings>
<cryptoNameMapping>
<cryptoClasses>
加密操作如下:
private void ProtectSection(string sectionName, string provider)
{
Configuration config =
WebConfigurationManager.
OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = config.GetSection(sectionName); if (section != null && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
config.Save();
}
}
<add key="strCnnNetcheck" value="server=maoning;uid=sa;pwd=hjhsd;database=netcheck"/>
<add key="limit" value="70"/>
value="server=maoning;uid=sa;pwd=jksdha;database=newzcpdm"/你可以把他弄为:value="server=m1aAoBncindg2;uid=ds2a1;pwd=jksdha;database=newzcpdm"/在通过字符编码基本上一般人是看不懂的;遇到那些太猛烈的人 你在厉害的他都能够给你搞出来,