分享:安全存放web项目数据库连接字符串

我的做法是这样:

1、在项目ABC的下面建目录Settings,里面有文件Settings.xml,其内容是:

1<section name="Settings">
2<key name="SQLServer" value="mySvr"></key>
3<key name="SQLDatabase" value="myDb"></key>
4<key name="SQLUID" value="myId"></key>
5<key name="SQLPWD" value="myPw"></key>
6</section>

当然,这里就是数据库连结的基本信息。

2、在项目的web.config中,加入:

1<configuration>   
2.....   
3<appsettings>
4<add key="SettingsFile" value=".\Settings\Settings.XML"></add>
5</appsettings>
6</configuration>

3、在Global.asax.cs中:

protected void Application_Start(Object sender, EventArgs e)
{
SetConnectionString();
}

private void SetConnectionString()
{
String sServer, sDB, sUID, sPWD, strSettingsFile;
strSettingsFile = System.Web.HttpContext.Current.Server.MapPath(" \\ABC\\ ") + System.Configuration.ConfigurationSettings.AppSettings["SettingsFile"];

Application["DatabaseConnectionString"] = "";

try
{
sServer = clsCommon.ReadSettings(strSettingsFile, "SQLServer");
sDB = clsCommon.ReadSettings(strSettingsFile, "SQLDatabase");
sUID = clsCommon.ReadSettings(strSettingsFile, "SQLUID");
sPWD = clsCommon.ReadSettings(strSettingsFile, "SQLPWD");
Application["DatabaseConnectionString"] = "Server=" + sServer.Trim() + ";Database=" + sDB.Trim() + ";uid=" + sUID.Trim() + ";pwd=" + sPWD.Trim() + ";";
}
catch(Exception excp)
{
throw(excp);
}
}

这里,从web.config中读到Setting.xml所在的相对路径,然后找到服务器上的文件,再读取其内容,就设定了Application级别的变量DatabaseConnectionString,当然,如果是要求各个session的连接字符串不一定相同,可以改成Session级别的。

4、在第3步中,用到的读取xml文件的函数实现如下:
using System;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web;
namespace ABC
{
///

1<summary>   
2/// Summary description for clsCommon.   
3/// </summary>

public class clsCommon: ABC
{
private const String NOTFOUND = "<

 1<nothing>&gt;";   
 2public clsCommon()   
 3{   
 4//   
 5// TODO: Add constructor logic here   
 6//   
 7} 
 8
 9static public String ReadSettings(String strSettingsFile , String sKey)   
10{   
11XmlTextReader xmlTR = new XmlTextReader(strSettingsFile);   
12XmlDocument m_xmlDocument = new XmlDocument();   
13m_xmlDocument.Load(xmlTR);   
14xmlTR.Close();   
15  
16String strResult;   
17strResult = GetSettingStr(m_xmlDocument, "Settings", sKey, ""); 
18
19return strResult;   
20} 
21
22static public String GetSettingStr( XmlDocument xmlDocument , String SectionName , String KeyName, String DefaultValue )   
23{   
24String sKeyValue ;   
25sKeyValue = _GetSetting(xmlDocument, SectionName, KeyName);   
26if (sKeyValue == NOTFOUND )   
27sKeyValue = DefaultValue;   
28return sKeyValue;   
29} 
30
31static public String _GetSetting(XmlDocument xmlDocument ,String SectionName ,String KeyName )   
32{   
33String sKeyValue;   
34XmlNode xnSection;   
35XmlNode xnKey ;   
36xnSection = xmlDocument.SelectSingleNode("//Section[@Name='" + SectionName + "']");   
37if(xnSection == null )   
38sKeyValue = NOTFOUND;   
39else   
40{   
41xnKey = xnSection.SelectSingleNode ("descendant::Key[@Name='" + KeyName + "']");   
42if( xnKey == null )   
43sKeyValue = NOTFOUND;   
44else   
45sKeyValue = xnKey.Attributes["Value"].Value;   
46}   
47xnKey = null;   
48xnSection = null;   
49return sKeyValue;   
50}   
51} 
52
53  
54总结:安全存放web项目的数据库连接字符串,可以把它保存在另一个目录的xml文件中,易于维护、更换,同时,可以设置此xml设置文件只允许asp_net用户访问,实现了安全保护。   
55
56
57|  回复人: ** cuike519(studing sps(修练中...))  ** ( ![两星\(中级\)](http://community.csdn.net/expert/images/rank/star2.gif) ) 信誉:100  |  2004-07-03 19:06:00  |  得分:  0   
58---|---|---|---  
59  
60支持!!! 
61
62  
63可是放在Web.config里面有什么不安全的?如果在Web.config里面不安全放在其他的目录里面就更不安全了!你可以做一个简单的试验,放一个xml文件和web.config在一起,web.config你打不开但是那个xml文件肯定可以打开!   
64
65
66回复人: ** athossmth(athos)  ** ( ![一星\(中级\)](http://community.csdn.net/expert/images/rank/star1.gif) ) 信誉:100  |  2004-07-03 19:24:00  |  得分:  0   
67---|---|---  
68  
69哪里哪里,当然了,一般在web.config中就足够了。 
70
71是这样的,我们这里的控制要求是,最后项目ProjectABC发布的目录是: 
72
73\\\ServerA\C$\APPS\ProjectABC\ 
74
75而连接字符串要放到 
76
77\\\ServerA\C$\APPS\Settings\SettingABC.xml 
78
79里,在Project的IIS virtual directory之外,统一管理。   
80
81
82本文原发表于 http://community.csdn.net/Expert/topic/3143/3143428.xml</nothing>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus