modifying web.config programmatically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vadim

    modifying web.config programmatically

    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.

    ConfigurationSe ttings.appsetti ngs allows to read web.config sections but how
    to write there?

    Thank you

    Vadim


  • Curt_C [MVP]

    #2
    Re: modifying web.config programmaticall y

    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" <vadim@domain.c om> wrote in message
    news:icvoc.4306 81$Pk3.140361@p d7tw1no...[color=blue]
    > Hi,
    >
    > Is there a .Net control available that allows to write into web.config[/color]
    file[color=blue]
    > 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[/color]
    the[color=blue]
    > encrypted entries is a simple winform app.
    >
    > ConfigurationSe ttings.appsetti ngs allows to read web.config sections but[/color]
    how[color=blue]
    > to write there?
    >
    > Thank you
    >
    > Vadim
    >
    >[/color]


    Comment

    • Ken Cox [Microsoft MVP]

      #3
      Re: modifying web.config programmaticall y

      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



      "vadim" <vadim@domain.c om> wrote in message
      news:icvoc.4306 81$Pk3.140361@p d7tw1no...[color=blue]
      > 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.
      >
      > ConfigurationSe ttings.appsetti ngs allows to read web.config sections but
      > how
      > to write there?
      >
      > Thank you
      >
      > Vadim
      >
      >[/color]

      Comment

      • Kevin Spencer

        #4
        Re: modifying web.config programmaticall y

        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" <vadim@domain.c om> wrote in message
        news:icvoc.4306 81$Pk3.140361@p d7tw1no...[color=blue]
        > Hi,
        >
        > Is there a .Net control available that allows to write into web.config[/color]
        file[color=blue]
        > 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[/color]
        the[color=blue]
        > encrypted entries is a simple winform app.
        >
        > ConfigurationSe ttings.appsetti ngs allows to read web.config sections but[/color]
        how[color=blue]
        > to write there?
        >
        > Thank you
        >
        > Vadim
        >
        >[/color]


        Comment

        • John Timney \(Microsoft MVP\)

          #5
          Re: modifying web.config programmaticall y

          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" <vadim@domain.c om> wrote in message
          news:icvoc.4306 81$Pk3.140361@p d7tw1no...[color=blue]
          > Hi,
          >
          > Is there a .Net control available that allows to write into web.config[/color]
          file[color=blue]
          > 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[/color]
          the[color=blue]
          > encrypted entries is a simple winform app.
          >
          > ConfigurationSe ttings.appsetti ngs allows to read web.config sections but[/color]
          how[color=blue]
          > to write there?
          >
          > Thank you
          >
          > Vadim
          >
          >[/color]


          Comment

          • vadim

            #6
            Re: modifying web.config programmaticall y

            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" <vadim@domain.c om> wrote in message
            news:icvoc.4306 81$Pk3.140361@p d7tw1no...[color=blue]
            > Hi,
            >
            > Is there a .Net control available that allows to write into web.config[/color]
            file[color=blue]
            > 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[/color]
            the[color=blue]
            > encrypted entries is a simple winform app.
            >
            > ConfigurationSe ttings.appsetti ngs allows to read web.config sections but[/color]
            how[color=blue]
            > to write there?
            >
            > Thank you
            >
            > Vadim
            >
            >[/color]


            Comment

            • Jason DeFontes

              #7
              Re: modifying web.config programmaticall y

              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.Lo ad("web.config" );

              XmlNodeList oList =
              configXmlDoc.Se lectNodes("conf iguration/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 == "ConnectionStri ng")
              {
              // parse conn string
              Regex connStringRegex = new
              Regex("SERVER=( .*);DATABASE=(. *);UID=(.*);PWD =(.*);",
              RegexOptions.Ig noreCase);
              Match match = connStringRegex .Match(oValue.V alue);
              dbServerText.Te xt = 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.Se lectNodes("conf iguration/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 == "ConnectionStri ng")
              {
              oValue.Value =
              string.Format(" SERVER={0};DATA BASE={1};UID={2 };PWD={3};",
              dbServerText.Te xt, dbNameText.Text , dbUserText.Text ,
              dbPasswordText. Text);
              }
              }

              configXmlDoc.Sa ve("Web.config" );


              -Jason

              vadim wrote:
              [color=blue]
              > 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" <vadim@domain.c om> wrote in message
              > news:icvoc.4306 81$Pk3.140361@p d7tw1no...
              >[color=green]
              >>Hi,
              >>
              >>Is there a .Net control available that allows to write into web.config[/color]
              >
              > file
              >[color=green]
              >>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[/color]
              >
              > the
              >[color=green]
              >>encrypted entries is a simple winform app.
              >>
              >>Configuration Settings.appset tings allows to read web.config sections but[/color]
              >
              > how
              >[color=green]
              >>to write there?
              >>
              >>Thank you
              >>
              >>Vadim
              >>
              >>[/color]
              >
              >
              >[/color]

              Comment

              • vadim

                #8
                Re: modifying web.config programmaticall y

                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" <jason@defontes .com> wrote in message
                news:OEAOIqJOEH A.2244@tk2msftn gp13.phx.gbl...[color=blue]
                > 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.Lo ad("web.config" );
                >
                > XmlNodeList oList =
                > configXmlDoc.Se lectNodes("conf iguration/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 == "ConnectionStri ng")
                > {
                > // parse conn string
                > Regex connStringRegex = new
                > Regex("SERVER=( .*);DATABASE=(. *);UID=(.*);PWD =(.*);",
                > RegexOptions.Ig noreCase);
                > Match match = connStringRegex .Match(oValue.V alue);
                > dbServerText.Te xt = 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.Se lectNodes("conf iguration/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 == "ConnectionStri ng")
                > {
                > oValue.Value =
                > string.Format(" SERVER={0};DATA BASE={1};UID={2 };PWD={3};",
                > dbServerText.Te xt, dbNameText.Text , dbUserText.Text ,
                > dbPasswordText. Text);
                > }
                > }
                >
                > configXmlDoc.Sa ve("Web.config" );
                >
                >
                > -Jason
                >
                > vadim wrote:
                >[color=green]
                > > Thank you, everybody for your replies.
                > >
                > > The idea is to configure database connections and write the[/color][/color]
                configuration[color=blue][color=green]
                > > 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[/color][/color]
                installed[color=blue][color=green]
                > > 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[/color][/color]
                user[color=blue][color=green]
                > > 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[/color][/color]
                was[color=blue][color=green]
                > > easy to write encrypted database connection settings into ini files from[/color][/color]
                a[color=blue][color=green]
                > > program that does encryption.
                > >
                > > The information is static because the db connections will be very seldom
                > > reconfigured.
                > >
                > > "vadim" <vadim@domain.c om> wrote in message
                > > news:icvoc.4306 81$Pk3.140361@p d7tw1no...
                > >[color=darkred]
                > >>Hi,
                > >>
                > >>Is there a .Net control available that allows to write into web.config[/color]
                > >
                > > file
                > >[color=darkred]
                > >>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[/color]
                > >
                > > the
                > >[color=darkred]
                > >>encrypted entries is a simple winform app.
                > >>
                > >>Configuration Settings.appset tings allows to read web.config sections but[/color]
                > >
                > > how
                > >[color=darkred]
                > >>to write there?
                > >>
                > >>Thank you
                > >>
                > >>Vadim
                > >>
                > >>[/color]
                > >
                > >
                > >[/color][/color]


                Comment

                • Jason DeFontes

                  #9
                  Re: modifying web.config programmaticall y

                  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:
                  [color=blue]
                  > 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" <jason@defontes .com> wrote in message
                  > news:OEAOIqJOEH A.2244@tk2msftn gp13.phx.gbl...
                  >[color=green]
                  >>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.confi g");
                  >>
                  >>XmlNodeList oList =
                  >>configXmlDoc. SelectNodes("co nfiguration/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 == "ConnectionStri ng")
                  >> {
                  >> // parse conn string
                  >> Regex connStringRegex = new
                  >>Regex("SERVER =(.*);DATABASE= (.*);UID=(.*);P WD=(.*);",
                  >> RegexOptions.Ig noreCase);
                  >> Match match = connStringRegex .Match(oValue.V alue);
                  >> dbServerText.Te xt = 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("co nfiguration/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 == "ConnectionStri ng")
                  >> {
                  >> oValue.Value =
                  >>string.Format ("SERVER={0};DA TABASE={1};UID= {2};PWD={3};",
                  >> dbServerText.Te xt, dbNameText.Text , dbUserText.Text ,
                  >>dbPasswordTex t.Text);
                  >> }
                  >>}
                  >>
                  >>configXmlDoc. Save("Web.confi g");
                  >>
                  >>
                  >>-Jason
                  >>
                  >>vadim wrote:
                  >>
                  >>[color=darkred]
                  >>>Thank you, everybody for your replies.
                  >>>
                  >>>The idea is to configure database connections and write the[/color][/color]
                  >
                  > configuration
                  >[color=green][color=darkred]
                  >>>informatio n into web.config, the utility will be used by end users. They
                  >>>will install the ASP.NET application and the utility will be also[/color][/color]
                  >
                  > installed
                  >[color=green][color=darkred]
                  >>>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[/color][/color]
                  >
                  > user
                  >[color=green][color=darkred]
                  >>>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[/color][/color]
                  >
                  > was
                  >[color=green][color=darkred]
                  >>>easy to write encrypted database connection settings into ini files from[/color][/color]
                  >
                  > a
                  >[color=green][color=darkred]
                  >>>program that does encryption.
                  >>>
                  >>>The information is static because the db connections will be very seldom
                  >>>reconfigured .
                  >>>
                  >>>"vadim" <vadim@domain.c om> wrote in message
                  >>>news:icvoc.4 30681$Pk3.14036 1@pd7tw1no...
                  >>>
                  >>>
                  >>>>Hi,
                  >>>>
                  >>>>Is there a .Net control available that allows to write into web.config
                  >>>
                  >>>file
                  >>>
                  >>>
                  >>>>appsettin gs section?
                  >>>>
                  >>>>The idea is to create encrypted user name and password for database
                  >>>>connectio n and then use them from ASP.Net. The program that will create
                  >>>
                  >>>the
                  >>>
                  >>>
                  >>>>encrypted entries is a simple winform app.
                  >>>>
                  >>>>Configurati onSettings.apps ettings allows to read web.config sections but
                  >>>
                  >>>how
                  >>>
                  >>>
                  >>>>to write there?
                  >>>>
                  >>>>Thank you
                  >>>>
                  >>>>Vadim
                  >>>>
                  >>>>
                  >>>
                  >>>
                  >>>[/color][/color]
                  >
                  >[/color]

                  Comment

                  Working...