By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

modifying web.config programmatically

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
as long as the site isn't being accessed just use/edit as you would any text
file.

--
Curt Christianson
Owner/Lead Developer, DF-Software
Site: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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

Nov 18 '05 #2

P: n/a
You might want to investigate using the registry to store the sensitive
information.

HOW TO: Use the ASP.NET Utility to Encrypt Credentials and Session State
Connection Strings

http://support.microsoft.com/default...b;en-us;329290

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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


Nov 18 '05 #3

P: n/a
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.

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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

Nov 18 '05 #4

P: n/a
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
"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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

Nov 18 '05 #5

P: n/a
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.

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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

Nov 18 '05 #6

P: n/a
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[i].Attributes["key"];
XmlAttribute oValue = oList[i].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[i].Attributes["key"];
XmlAttribute oValue = oList[i].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.

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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


Nov 18 '05 #7

P: n/a
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" <ja***@defontes.com> wrote in message
news:OE**************@tk2msftngp13.phx.gbl...
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[i].Attributes["key"];
XmlAttribute oValue = oList[i].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[i].Attributes["key"];
XmlAttribute oValue = oList[i].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.

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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


Nov 18 '05 #8

P: n/a
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

vadim wrote:
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" <ja***@defontes.com> wrote in message
news:OE**************@tk2msftngp13.phx.gbl...
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[i].Attributes["key"];
XmlAttribute oValue = oList[i].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[i].Attributes["key"];
XmlAttribute oValue = oList[i].Attributes["value"];
if(oKey.Value == "ConnectionString")
{
oValue.Value =
string.Format("SERVER={0};DATABASE={1};UID={2};P WD={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.

"vadim" <va***@domain.com> wrote in message
news:icvoc.430681$Pk3.140361@pd7tw1no...
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



Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.