kamiar3001
kamiar3001

Reputation: 2676

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.

Upvotes: 3

Views: 1074

Answers (1)

Aristos
Aristos

Reputation: 66641

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.

Upvotes: 3

Related Questions