Securely store the Web Project database connection string

Source: Internet
Author: User
web| Security | data | database | database Connection | project | string
My approach is this:

1, under the project ABC to build a directory settings, there are file Settings.xml, its contents are:
<?xml version= "1.0" encoding= "Utf-8"?>
<section name= "Settings" >
<key name= "SQL Server" value= "Mysvr"/>
<key name= "SQLDatabase" value= "MyDb"/>
<key name= "Sqluid" value= "MyId"/>
<key name= "sqlpwd" value= "MYPW"/>
</Section>
Of course, this is the basic information about the database connection.

2. In the web.config of the project, add:
<configuration>
.....
<appSettings>
<add key= "SettingsFile" value= ". Ettings ettings. XML "></add>
</appSettings>
</configuration>

3, in the 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, "SQL Server");
SDB = Clscommon.readsettings (Strsettingsfile, "sqldatabase");
SUID = Clscommon.readsettings (Strsettingsfile, "Sqluid");
Spwd = Clscommon.readsettings (Strsettingsfile, "sqlpwd");
application["databaseconnectionstring"] = "server=" + sserver.trim () + ";D atabase=" + sdb.trim () + "uid=" + sUID.Trim () + ";p wd=" + spwd.trim () + ";";
}
catch (Exception EXCP)
{
throw (EXCP);
}
}

Over here Read from web.config to the relative path where Setting.xml is located, then locate the file on the server, and then read its contents, set the application level variable databaseconnectionstring, of course, if the connection string for each session is not Must be the same, can be changed to the session level.

4. In the 3rd step, the function used to read the XML file is implemented as follows:
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;
}
}


Summary: Safe storage of Web Project database connection string, you can save it in another directory in the XML file, easy to maintain, replace, at the same time, you can set up this XML settings file to allow only asp_net users access to achieve security.


Reply person: cuike519 (studing SPS) (in practice ...) ) () Credit: 100 2004-07-03 19:06:00 Score: 0

Support!!!


But what's not safe in the web.config? It's even less safe to put it in the other directory if it's unsafe in Web.config! You can do a simple experiment, put an XML file with web.config, web.config you can't open it, but that XML file can definitely open!


Reply Person: athossmth (Athos) () Prestige: 100 2004-07-03 19:24:00 Score: 0

Where, of course, generally in the web.config is enough.

Yes, here's the control requirement, the final project PROJECTABC published directory is:

\ ervera\c$\apps\projectabc\

And the connection string is placed in the

\ Ervera\c$\apps ettings Ettingabc.xml

, unified management is in addition to the IIS virtual directory for project.




This article was originally published in Http://community.csdn.net/Expert/topic/3143/3143428.xml


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.