Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day

I have googled to no avail and I was wondering if anyone can help me. I need to change the web.config of a remote site in c#. Then encrypt the web.config section and be able to decrypt it to make changes again. We are running iis7.

I have managed to make changes to the remote web.config with no problem. But as soon as I encrypt the Connection string section and then try to update it. I Get "configuration section encryption is not supported iis7" when the web.config is Encrypted when it is not Encrypted it works.

The Code I am using is
C#
ServerManager iisManager = new ServerManager();
  Microsoft.Web.Administration.Configuration config = iisManager.GetWebConfiguration(ddListSites.SelectedItem.Value.ToString());

                    ConfigurationSection connectionStringsSection = config.GetSection("connectionStrings");
                    ConfigurationElementCollection appSettingsCollection = connectionStringsSection.GetCollection();

                    foreach (ConfigurationElement conlist in appSettingsCollection)
                    {
                        string Name = conlist.Attributes[1].Value.ToString();
                        string Constring = conlist.Attributes[0].Value.ToString();
                        string ConstringAttributeName = conlist.Attributes[0].Name.ToString();

                        if (Name == ddlistConnections.SelectedItem.Text)
                        {

                            conlist.Attributes[0].Value = newconnection;
                            iisManager.CommitChanges();

                        }

                    }

I cannot use the following because it is not a local web.config


C#
Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
       //open the connection strings section
       ConfigurationSection section = config.GetSection("connectionStrings");

       //check to make sure it is not already encrypted
       if (!section.SectionInformation.IsProtected)
       {
           //encrypt it with RSA Protection
           section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
           //save the config file
           config.Save();
       }


The requirement is that the web.config must be encrypted

Any help or pointers would be gladly appreciated.

Kind regards
Divan
Posted
Updated 1-Jul-15 1:06am
v2
Comments
mrDivan 2-Jul-15 3:41am    
Anyone else have any ideas

1 solution

IIS7 understand pre-encrypted sections, but do not support section encryption via the management objects...
Section encryption supported only manually - via aspnet_regiis! You can manage attribute encryption via management objects, if it helps...
 
Share this answer
 
Comments
mrDivan 1-Jul-15 7:11am    
Hallo thank you for your quick response. Do you perhaps have an example of how to encrypt an attribute.

Kind regards
Divan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900