标题:
asp.net2.0如何加密数据库联接字符串
[打印本页]
作者:
lilcy88
时间:
2008-5-28 10:41
标题:
asp.net2.0如何加密数据库联接字符串
asp.net2.0如何加密数据库联接字符串
0 b/ q8 Z. ]8 O' P! r
在asp.net2.0中,发布网站时,加密web.config,这样可以有效保证数据库用户和密码安全,其步骤如下:
2 t J- H0 v) |! }. z
1.添加密钥
" Q+ g2 g8 ]0 n' l/ s) U( z
执行:c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pc "hnlaw" -exp
- U, q8 g1 ~, l- m/ }9 \
其中"hnlaw"为密钥名称
/ }. x6 x8 K4 p* R2 Q$ ]2 P
2.添加web.config节点
/ d- i- f% U# a% P+ @
在web.config的<configuration></configuration>之间添加:
% R- C4 t" }. I/ c% j9 t; ]8 K
<configprotecteddata>
. L+ o9 V& t0 [3 ^7 m3 A9 Y3 P
<providers>
: L2 g+ o ?. i$ ~3 a `" K* Q( c
<add keycontainername="hnlaw" usemachinecontainer="true" description="uses rsacryptoserviceprovider to encrypt and
# r& [, Y4 q/ |# m- M+ ]
decrypt" name="hnlaw" type="system.configuration.rsaprotectedconfigurationprovider,system.configuration, version=2.0.0.0,
* k# D' C# D, m: X9 W
culture=neutral, publickeytoken=b03f5f7f11d50a3a" />
$ T5 s5 Q9 v; @% ~# l
</providers>
) N) e6 y1 r' A4 C
</configprotecteddata>
1 u& G* s1 B: s8 _& N1 K- M
注意:这里keycontainername="hnlaw"和name="hnlaw"分别表示你的密钥名称;
! n$ B; T% w$ P0 ?- B
3.加密web.config
$ [- F* \. d8 f3 T: j
到网站根目录添加一个批处理文件enweb.bat,内容如下:
7 e2 p; O# y& V( @, ]: A& G8 x
@echo off
% Q4 J& ]5 A( d: c; Y5 K
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "e:\hs studio\donet2\hnlawyer" -prov
7 z0 W& A9 `& Y- F* @
"hnlaw"
2 z7 E8 S) X3 u9 X! r
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "connectionstrings" "e:\hs studio\donet2\hnlawyer" -prov
: g- `' ?4 b7 Q0 E2 m
"hnlaw"
& L3 \ m& a0 ]" [! ?
pause
& P* p/ r, \( S# d; _
注册上面的的路径和名称!
) n* x, E9 {" N+ B
运行后出现成功!
$ n: @) t' h$ v3 y
4.解密
/ D2 D5 {: S% g6 Z
同样到网站根目录添加一个批处理文件deweb.bat,内容如下:
, O5 r3 C. h$ L: J5 T: k9 _. b
@echo off
$ Q- g* {. B9 I' c' F, v
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pef "system.web/identity" "e:\hs studio\donet2\hnlawyer"
# ]; \6 |9 q2 P+ d6 R: H
c:\windows\microsoft.net\framework\v2.0.50727\aspnet_regiis -pdf "connectionstrings" "e:\hs studio\donet2\hnlawyer"
0 D+ a. J ?1 t) R
pause
; M4 }" h. x$ |" `6 O
最后就是要注意:做完后找到c:\documents and settings\all users\application data\microsoft\crypto\rsa\machinekeys目录下,找到生成
0 M* p- S& J7 B: P# ?5 F: S
的密钥文件(可按时间查找),给上network service可读取权限,不然会出现error message from the provider: the rsa key container could
& r: P+ g5 J; Z- K- ]8 {
not be opened.无法读取
) \% F1 a6 K. B) c' J8 z
! y1 S+ Z# f& q p
这样可能会出现,如果没有自己的服务器,没有权限修改machinekeys目录顺便向你推荐一款高速稳定的时代互联智强商务C型 +.CN域名,原价1952元/年,现
j0 p, w& {2 n0 V; z
价只需1588元/年,还免费送域名,CDN网站加速器 送繁简通网络简装版 ,联付宝网上支付,我觉得特值,特向大家推荐!有兴趣的朋友可
) J* T! e" g0 |6 c& f
到
http://www.now.cn/vhost
申请试试! 电话:0756-2623871 QQ: 168075865 MSN:
north888@now.net.cn
* k& \/ F6 \6 Y, [9 T* t+ }
全球免费咨询电话
http://www.now.cn/callcenter/call.net?LineName=55
欢迎光临 捌玖网络工作室 (http://89w.org/)
Powered by Discuz! 7.2