小菜一碟:安全存放web项目数据库连接字符串

athossmth 2004-07-03 06:55:04
我的做法是这样:

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用户访问,实现了安全保护。
...全文
248 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
athossmth 2004-07-03
  • 打赏
  • 举报
回复
哪里哪里,当然了,一般在web.config中就足够了。

是这样的,我们这里的控制要求是,最后项目ProjectABC发布的目录是:

\\ServerA\C$\APPS\ProjectABC
而连接字符串要放到

\\ServerA\C$\APPS\Settings\SettingABC.xml

里,在Project的IIS virtual directory之外,统一管理。

这不是程序写完了嘛,扔在硬盘商业是发霉,就放上来供大家参考:)
cuike519 2004-07-03
  • 打赏
  • 举报
回复
支持!!!


可是放在Web.config里面有什么不安全的?如果在Web.config里面不安全放在其他的目录里面就更不安全了!你可以做一个简单的试验,放一个xml文件和web.config在一起,web.config你打不开但是那个xml文件肯定可以打开!

不过还是支持!现在论坛里这样的好人越来越少!我只是想讨论一下没有别的意思!希望搂主不要生气!

:-)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧