How to Write to Web.config in Medium Trust

Leader ●2 ●9 ●43
calendar_today • schedule1 min read

If you’re working with an older ASP.NET application running under Medium Trust, you may run into a common problem: your application can read Web.config, but attempting to modify it at runtime fails with a security or permission exception.

The important point is that Medium Trust is designed to restrict applications from modifying configuration files. Simply using Server.MapPath() and File.WriteAllText() is not a reliable solution—and in a hosted environment, it will generally be blocked by filesystem permissions or the trust policy.

What can you do?

If you need to change configuration dynamically, consider these approaches:

  1. Store dynamic settings outside Web.config

Instead of modifying Web.config, use a data store such as a database or another configuration mechanism that your application is allowed to write to.

// Example: store application settings in a database
string value = GetSettingFromDatabase("MySetting");

  1. Use an application-level configuration file

For settings that need to be writable, keep them in a separate file, provided your hosting environment allows your application to create and modify that file.

string path = Server.MapPath("~/App_Data/settings.xml");

File.WriteAllText(path, "Value");

App_Data is commonly used for application data because it isn't intended to be served directly to clients.

  1. Change Web.config during deployment

If the configuration really belongs in Web.config, the safest approach is usually to modify it as part of your deployment process rather than from application code.

For example:

Your deployment process can generate or transform the appropriate configuration for each environment.

What about ConfigurationManager?

You can read configuration values normally:

string value =

ConfigurationManager.AppSettings["MySetting"];

But being able to read configuration doesn't mean the application has permission to write the physical Web.config file.

Bottom line

Under Medium Trust, don't design your application around runtime modification of Web.config. If a value needs to change while the application is running, keep that value in a writable data store or separate configuration file. If it belongs in Web.config, update it during deployment.

This approach is more secure, more predictable, and works better with hosted ASP.NET environments.

🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

Delivering Database Changes

Steve Fentonverified - Jul 22

How AI Is Changing Web Development, and What Developers Still Need to Understand

Daniel Decker - Nov 10

Your AI Doesn't Just Write Tests. It Runs Them Too.

Kevin Martinez - May 12

# One Config Key Was Enough: What the Meta Muse Zero-Day Teaches About Agent Trust Boundaries

alessandro_pignati - Nov 2

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelski - Mar 19
chevron_left
2.8k Points • 54 Badges
4Posts
67Comments
65Connections
As the Content Manager at CoderLegion, I help maintain high quality content and create a positive pu... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!