开发者

encrypting 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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜