我的做法是这样: 1、在项目ABC的下面建目录Settings,里面有文件Settings.xml,其内容是: <?xml version="1.0" encoding="utf-8" ?> <Section Name="Settings"> <Key Name="SQLServer" Value="mySvr" /> <Key Name="SQLDatabase" Value="myDb" /> <Key Name="SQLUID" Value="myId" /> <Key Name="SQLPWD" Value="myPw" /> </Section> 当然,这里就是数据库连结的基本信息。 2、在项目的web.config中,加入: <configuration> ..... <appSettings> <add key="SettingsFile" value=".\Settings\Settings.XML"></add> </appSettings> </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 { /// <summary> /// Summary description for clsCommon. /// </summary> public class clsCommon: ABC { private const String NOTFOUND = "<<nothing>>"; public clsCommon() { // // TODO: Add constructor logic here // } static public String ReadSettings(String strSettingsFile , String sKey) { XmlTextReader xmlTR = new XmlTextReader(strSettingsFile); XmlDocument m_xmlDocument = new XmlDocument(); m_xmlDocument.Load(xmlTR); xmlTR.Close(); String strResult; strResult = GetSettingStr(m_xmlDocument, "Settings", sKey, ""); return strResult; } static public String GetSettingStr( XmlDocument xmlDocument , String SectionName , String KeyName, String DefaultValue ) { String sKeyValue ; sKeyValue = _GetSetting(xmlDocument, SectionName, KeyName); if (sKeyValue == NOTFOUND ) sKeyValue = DefaultValue; return sKeyValue; } static public String _GetSetting(XmlDocument xmlDocument ,String SectionName ,String KeyName ) { String sKeyValue; XmlNode xnSection; XmlNode xnKey ; xnSection = xmlDocument.SelectSingleNode("//Section[@Name='" + SectionName + "']"); if(xnSection == null ) sKeyValue = NOTFOUND; else { xnKey = xnSection.SelectSingleNode ("descendant::Key[@Name='" + KeyName + "']"); if( xnKey == null ) sKeyValue = NOTFOUND; else sKeyValue = xnKey.Attributes["Value"].Value; } xnKey = null; xnSection = null; return sKeyValue; } } 总结:安全存放web项目的数据库连接字符串,可以把它保存在另一个目录的xml文件中,易于维护、更换,同时,可以设置此xml设置文件只允许asp_net用户访问,实现了安全保护。
支持!!! 可是放在Web.config里面有什么不安全的?如果在Web.config里面不安全放在其他的目录里面就更不安全了!你可以做一个简单的试验,放一个xml文件和web.config在一起,web.config你打不开但是那个xml文件肯定可以打开!
哪里哪里,当然了,一般在web.config中就足够了。 是这样的,我们这里的控制要求是,最后项目ProjectABC发布的目录是: \\ServerA\C$\APPS\ProjectABC\ 而连接字符串要放到 \\ServerA\C$\APPS\Settings\SettingABC.xml 里,在Project的IIS virtual directory之外,统一管理。
本文原发表于 http://community.csdn.net/Expert/topic/3143/3143428.xml 
|