Reputation: 9485
I am trying to update an appSetting in the web.config in an MVC3 app running in medium trust. It fails as soon as I try to call?
var config = WebConfigurationManager.OpenWebConfiguration("~");
Tried other variations but they all seem to throw this error.
System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ failed.
Anyone know how to do it in medium trust? Any help would be greatly appreciated.
Upvotes: 0
Views: 359
Reputation: 9485
Here is a method for doing it in medium trust
http://www.mvcforum.com/blog/2012/9/updating-appsettings-in-the-webconfig-in-medium-trust/
Upvotes: 0
Reputation: 3323
Short answer: it's not possible in Medium Trust.
Long answer: the web.config actually inherits a bunch of settings down from the machine-level web.configs. As a result, when you access the Configuration & AppSettings classes from opening a given .config file, it's read-only (cause that's what the system lets you do), but will throw the FileIO permission when trying to write, as the classes have to determine somewhere up the call stack that they have permission to write to the correct config (folder, inherited, or machine-level) prior to writing your new setting (depending on where the setting was set in the inheritance hierarchy). N.B. This was meant to be fixed in .NET4... I remember seeing a link on the Microsoft Connect site somewhere about it.
Summary: Use XML/XSLT parsing.
Upvotes: 2