国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

安全存放web項(xiàng)目數(shù)據(jù)庫連接字符串

2024-07-21 02:16:00
字體:
供稿:網(wǎng)友

我的做法是這樣:

1、在項(xiàng)目abc的下面建目錄settings,里面有文件settings.xml,其內(nèi)容是:
<?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>
當(dāng)然,這里就是數(shù)據(jù)庫連結(jié)的基本信息。

2、在項(xiàng)目的web.config中,加入:
<configuration>
.....
  <appsettings>
    <add key="settingsfile" value=". ettings ettings.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("http://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所在的相對(duì)路徑,然后找到服務(wù)器上的文件,再讀取其內(nèi)容,就設(shè)定了application級(jí)別的變量databaseconnectionstring,當(dāng)然,如果是要求各個(gè)session的連接字符串不一定相同,可以改成session級(jí)別的。

4、在第3步中,用到的讀取xml文件的函數(shù)實(shí)現(xiàn)如下:
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("http://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;
}
}


總結(jié):安全存放web項(xiàng)目的數(shù)據(jù)庫連接字符串,可以把它保存在另一個(gè)目錄的xml文件中,易于維護(hù)、更換,同時(shí),可以設(shè)置此xml設(shè)置文件只允許asp_net用戶訪問,實(shí)現(xiàn)了安全保護(hù)。


回復(fù)人: cuike519(studing sps(修練中...)) ( ) 信譽(yù):100 2004-07-03 19:06:00 得分: 0

支持!!!


可是放在web.config里面有什么不安全的?如果在web.config里面不安全放在其他的目錄里面就更不安全了!你可以做一個(gè)簡(jiǎn)單的試驗(yàn),放一個(gè)xml文件和web.config在一起,web.config你打不開但是那個(gè)xml文件肯定可以打開!


回復(fù)人: athossmth(athos) ( ) 信譽(yù):100 2004-07-03 19:24:00 得分: 0

哪里哪里,當(dāng)然了,一般在web.config中就足夠了。

是這樣的,我們這里的控制要求是,最后項(xiàng)目projectabc發(fā)布的目錄是:

/ ervera/c$/apps/projectabc/

而連接字符串要放到

/ ervera/c$/apps ettings ettingabc.xml

里,在project的iis virtual directory之外,統(tǒng)一管理。




本文原發(fā)表于 http://community.csdn.net/expert/topic/3143/3143428.xml
注冊(cè)會(huì)員,創(chuàng)建你的web開發(fā)資料庫,
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 册亨县| 北宁市| 文安县| 台南市| 遵义市| 广水市| 法库县| 涡阳县| 甘洛县| 桃园市| 康马县| 安新县| 曲周县| 若羌县| 佛教| 微博| 德钦县| 双流县| 襄樊市| 广饶县| 扎兰屯市| 林周县| 土默特左旗| 伊宁市| 城市| 元氏县| 攀枝花市| 临西县| 乌海市| 驻马店市| 遂溪县| 新邵县| 宜良县| 惠州市| 灯塔市| 南昌市| 甘肃省| 固镇县| 谷城县| 烟台市| 济南市|