返回列表 发帖

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串* }8 s" v* t( b/ p4 s6 m# y
在asp.net2.0中,发布网站时,加密web.config,这样可以有效保证数据库用户和密码安全,其步骤如下:( ]0 K& i# D+ w1 C
1.添加密钥
  k  X, C7 R7 q  g" _* q+ m执行:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pc "hnlaw" -exp4 F( c5 x% K# a; }+ i
其中"hnlaw"为密钥名称
" a1 g; t& q% x0 u2 V2.添加web.config节点( t: _* [: c" p! ?$ ?7 q" a+ O
在web.config的<configuration></configuration>之间添加:
: u7 Y/ i% c/ h' _<configprotecteddata>
" ^9 t  U: `  o5 I4 A. ^  <providers>
+ j  `/ M  `7 `% X0 ?, I   <add keycontainername="hnlaw" usemachinecontainer="true" description="uses rsacryptoserviceprovider to encrypt and
$ t+ x0 ?' g  Zdecrypt" name="hnlaw" type="system.configuration.rsaprotectedconfigurationprovider,system.configuration, version=2.0.0.0, % ?! m* `1 r8 {) G* r
culture=neutral, publickeytoken=b03f5f7f11d50a3a" />3 K1 u8 C4 Q6 m7 G5 n+ b$ O6 V% U
  </providers>1 z# Z0 B" E2 q4 ~: \
</configprotecteddata>
1 Y; _* {! P* L* |注意:这里keycontainername="hnlaw"和name="hnlaw"分别表示你的密钥名称;
& ]$ x" `- g9 p5 X* j* z& ]! ?3.加密web.config  c* o, F6 H/ W' s9 ]( [' @) G" O
到网站根目录添加一个批处理文件enweb.bat,内容如下:
6 [- j! T8 s5 B7 h) V! H5 w@echo off( R$ I- X7 V( _
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "e:\hs  studio\donet2\hnlawyer" -prov
, ?0 X/ B/ i& U( j. y"hnlaw"
0 N( |# k" p) Z' V: t: K  c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "connectionstrings" "e:\hs  studio\donet2\hnlawyer" -prov
+ z9 A# m( H: U9 A"hnlaw"
6 s* Q" w0 f4 X% j5 Vpause+ U0 {/ @* M3 K: \: u" `: Y
注册上面的的路径和名称!' c4 J/ Y' e1 s4 h6 E3 y
运行后出现成功!* {$ t" b) C& z3 F# V; m3 N
4.解密2 g( V) S# g" O/ |7 }
同样到网站根目录添加一个批处理文件deweb.bat,内容如下:
, z3 p" |- G, ^7 V0 @' i  C@echo off
9 a' M$ N* e/ @% c7 N& {3 Cc:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "e:\hs  studio\donet2\hnlawyer"" {, n8 d( X2 z9 e$ n; V3 x
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pdf "connectionstrings" "e:\hs  studio\donet2\hnlawyer"
2 e  l& i* N) N5 ?! ?* _0 Gpause* ]5 ~2 a' D& M9 f, O
最后就是要注意:做完后找到c:\documents and settings\all users\application data\microsoft\crypto\rsa\machinekeys目录下,找到生成
8 q- o% O4 R- K- j# V# W的密钥文件(可按时间查找),给上network service可读取权限,不然会出现error message from the provider: the rsa key container could
" l& _1 B" }4 G1 ]* B8 Vnot be opened.无法读取" D5 V1 Z& D9 [% b3 v3 e
" l7 r3 Y" r$ j" x  T
这样可能会出现,如果没有自己的服务器,没有权限修改machinekeys目录顺便向你推荐一款高速稳定的时代互联智强商务C型 +.CN域名,原价1952元/年,现
, V: {- O8 ^$ r. m) c6 S& V: i价只需1588元/年,还免费送域名,CDN网站加速器 送繁简通网络简装版 ,联付宝网上支付,我觉得特值,特向大家推荐!有兴趣的朋友可! U' }) U7 N1 O9 G. c0 m- q
http://www.now.cn/vhost申请试试!     电话:0756-2623871   QQ: 168075865   MSN:north888@now.net.cn# [4 [+ L- X& {0 o2 e% {2 i, p0 Y
全球免费咨询电话 http://www.now.cn/callcenter/call.net?LineName=55

返回列表
【捌玖网络】已经运行: