Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encrypting web.config

Here is my c# code :

System.Configuration.Configuration config =
    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");

ConfigurationSection section = config.GetSection("appSettings");
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;        
config.Save(ConfigurationSaveMode.Modified);
string sectionXml = section.SectionInformation.GetRawXml();

I need to encrypt web.config programatically, but it gives me error:

A configuration file cannot be created for the requested Configuration object.

like image 342
kamiar3001 Avatar asked Jan 27 '26 22:01

kamiar3001


1 Answers

The error is on the ~/web.config, the OpenWebConfiguration needs the full application path, not the name of the web.config as appears on web.

Try this (tested and working for the opening):

OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

or (base on msdn sample code)

OpenWebConfiguration(/web.config);

or even call it with null, as MSDN notes.

like image 175
Aristos Avatar answered Jan 30 '26 15:01

Aristos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!