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>
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 ();
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!
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