modifying web.config programmatically

V

vadim

Hi,

Is there a .Net control available that allows to write into web.config file
appsettings section?

The idea is to create encrypted user name and password for database
connection and then use them from ASP.Net. The program that will create the
encrypted entries is a simple winform app.

ConfigurationSettings.appsettings allows to read web.config sections but how
to write there?

Thank you

Vadim
 
K

Kevin Spencer

You don't. The configuration files are for static data. The place for
dynamic data is in memory, or a database of some kind.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
C

Curt_C [MVP]

as long as the site isn't being accessed just use/edit as you would any text
file.
 
J

John Timney \(Microsoft MVP\)

You would be changing the web.config file and restarting the web
application. If someone is using the web app - your winform app would
terminate their session. Web.config is for static data only.

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
V

vadim

Thank you, everybody for your replies.

The idea is to configure database connections and write the configuration
information into web.config, the utility will be used by end users. They
will install the ASP.NET application and the utility will be also installed
into the same directory. They will run the utility and it will create
encrypted entries in web.config. I want to make it as easy for the end user
as possible, I wouldn't want to go into registry.
I used to do the same thing with ini files for win 32 applications, it was
easy to write encrypted database connection settings into ini files from a
program that does encryption.

The information is static because the db connections will be very seldom
reconfigured.
 
J

Jason DeFontes

You'll have to read it in like any other xml file. This is chopped out
of a larger function, but you get the idea:

XmlDocument configXmlDoc = new XmlDocument();
configXmlDoc.Load("web.config");

XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
// parse conn string
Regex connStringRegex = new
Regex("SERVER=(.*);DATABASE=(.*);UID=(.*);PWD=(.*);",
RegexOptions.IgnoreCase);
Match match = connStringRegex.Match(oValue.Value);
dbServerText.Text = match.Groups[1].Captures[0].ToString();
dbNameText.Text = match.Groups[2].Captures[0].ToString();
dbUserText.Text = match.Groups[3].Captures[0].ToString();
dbPasswordText.Text = match.Groups[4].Captures[0].ToString();
}
}

// write the config settings
XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
oValue.Value =
string.Format("SERVER={0};DATABASE={1};UID={2};PWD={3};",
dbServerText.Text, dbNameText.Text, dbUserText.Text,
dbPasswordText.Text);
}
}

configXmlDoc.Save("Web.config");


-Jason
 
V

vadim

Hi Jason,

I figured out that I can just edit web.config as an ordinary xml file, but
your example will help me to do this,
also should I use full path to the web.config file in the load and save
calls?

Thank you very much

Vadim


Jason DeFontes said:
You'll have to read it in like any other xml file. This is chopped out
of a larger function, but you get the idea:

XmlDocument configXmlDoc = new XmlDocument();
configXmlDoc.Load("web.config");

XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
// parse conn string
Regex connStringRegex = new
Regex("SERVER=(.*);DATABASE=(.*);UID=(.*);PWD=(.*);",
RegexOptions.IgnoreCase);
Match match = connStringRegex.Match(oValue.Value);
dbServerText.Text = match.Groups[1].Captures[0].ToString();
dbNameText.Text = match.Groups[2].Captures[0].ToString();
dbUserText.Text = match.Groups[3].Captures[0].ToString();
dbPasswordText.Text = match.Groups[4].Captures[0].ToString();
}
}

// write the config settings
XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
oValue.Value =
string.Format("SERVER={0};DATABASE={1};UID={2};PWD={3};",
dbServerText.Text, dbNameText.Text, dbUserText.Text,
dbPasswordText.Text);
}
}

configXmlDoc.Save("Web.config");


-Jason
Thank you, everybody for your replies.

The idea is to configure database connections and write the configuration
information into web.config, the utility will be used by end users. They
will install the ASP.NET application and the utility will be also installed
into the same directory. They will run the utility and it will create
encrypted entries in web.config. I want to make it as easy for the end user
as possible, I wouldn't want to go into registry.
I used to do the same thing with ini files for win 32 applications, it was
easy to write encrypted database connection settings into ini files from a
program that does encryption.

The information is static because the db connections will be very seldom
reconfigured.
 
J

Jason DeFontes

Yeah, if your executable isn't in the same directory as the web.config
then you'll need the path, I just left that part out for simplicity.

-Jason
Hi Jason,

I figured out that I can just edit web.config as an ordinary xml file, but
your example will help me to do this,
also should I use full path to the web.config file in the load and save
calls?

Thank you very much

Vadim


You'll have to read it in like any other xml file. This is chopped out
of a larger function, but you get the idea:

XmlDocument configXmlDoc = new XmlDocument();
configXmlDoc.Load("web.config");

XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
// parse conn string
Regex connStringRegex = new
Regex("SERVER=(.*);DATABASE=(.*);UID=(.*);PWD=(.*);",
RegexOptions.IgnoreCase);
Match match = connStringRegex.Match(oValue.Value);
dbServerText.Text = match.Groups[1].Captures[0].ToString();
dbNameText.Text = match.Groups[2].Captures[0].ToString();
dbUserText.Text = match.Groups[3].Captures[0].ToString();
dbPasswordText.Text = match.Groups[4].Captures[0].ToString();
}
}

// write the config settings
XmlNodeList oList =
configXmlDoc.SelectNodes("configuration/appSettings/add");

for(int i = 0; i < oList.Count; i++)
{
XmlAttribute oKey = oList.Attributes["key"];
XmlAttribute oValue = oList.Attributes["value"];
if(oKey.Value == "ConnectionString")
{
oValue.Value =
string.Format("SERVER={0};DATABASE={1};UID={2};PWD={3};",
dbServerText.Text, dbNameText.Text, dbUserText.Text,
dbPasswordText.Text);
}
}

configXmlDoc.Save("Web.config");


-Jason

vadim wrote:

Thank you, everybody for your replies.

The idea is to configure database connections and write the
configuration
information into web.config, the utility will be used by end users. They
will install the ASP.NET application and the utility will be also
installed
into the same directory. They will run the utility and it will create
encrypted entries in web.config. I want to make it as easy for the end
user
as possible, I wouldn't want to go into registry.
I used to do the same thing with ini files for win 32 applications, it
was
easy to write encrypted database connection settings into ini files from
a
program that does encryption.

The information is static because the db connections will be very seldom
reconfigured.



Hi,

Is there a .Net control available that allows to write into web.config

file


appsettings section?

The idea is to create encrypted user name and password for database
connection and then use them from ASP.Net. The program that will create

the


encrypted entries is a simple winform app.

ConfigurationSettings.appsettings allows to read web.config sections but

how


to write there?

Thank you

Vadim

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top