开发者

encrypting web.config

开发者 https://www.devze.com 2023-03-10 14:55 出处:网络
Here is my c# code : System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(\"~/Web.config\");

Here is my c# code :

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

ConfigurationSection section = config.GetSection("app开发者_如何学GoSettings");
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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号