Share: Safe Store Web Project Database Connection Strings

xiaoxiao2021-03-05  27

My approach is this:

1. Built SETTINGS below the project ABC, there is file settings.xml, its content is:

Of course, here is the basic information of the database link.

2, in the web.config, join: .....

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);}} Here, read the relative path where setting.xml is located from the web.config, then find the file on the server, Read the content again, set the application-level variable DatabaseConnectionstring, of course, if it is required to require the connection string of the respective session, it can be changed to the session level.

4. In step 3, the functions of the read XML files are implemented as follows: use system.data; useing system.data.sqlclient; use system.data; us; Using system.Web; Namespace ABC {///

/// summary description for clscommon. /// 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; xnse = null;}}}

Summary: Securely store the web project database connection string, you can save it in the XML file of another directory, easy to maintain, replace, and you can set this XML setting file only allows ASP_NET users to access security.

Reply to: cuike519 (Studing SPS (in the middle ...)) () Reputation: 100 2004-07-03 19:06:00 Score: 0

stand by! ! !

But what is not safe in Web.config? If you are not safely placed in Web.config, you are not safe! You can make a simple trial, put an XML file and web.config, web.config you can't open but that XML file must open!

Reply to: ATHOSSMTH (ATHOS) () Reputation: 100 2004-07-03 19:24:00 Score: 0

Where is it, of course, usually in Web.config is enough.

This is the case, the control requirements we here are that the directory released by the final project ProjectAbc is:

// Servera / C $ / Apps / ProjectAbc /

And the connection string should be put

//Servera/c / pPPS/settings/settingabc.xml

In addition to the IIS Virtual Directory of Project.

转载请注明原文地址:https://www.9cbs.com/read-38691.html

New Post(0)