Medium trust issue in ASP.NET 2.0

This issue for us on [ASPnix] bit me to post this.  Medium trust issue in ASP.NET 2.0 is one of most common issues for developers on most of shared hosting environments.  Those of you who haven’t deployed an ASP.NET 2.0 application on a shared hosting server probably haven’t faced with this issue yet but you will soon!

ASP.NET 2.0 has been shipped with a new security feature to set the level of security trust on applications per server/application.  Server administrators can use this feature to increase their security.  By default .NET 2.0 users have Full level trust so you don’t see this on your development machines. But on most of shared hosting servers, administrators set this to Medium.  This will cause to some issues for applications if they use some special parts of .NET 2.0 in their code.  You can override this configuration in your web.config and change it to upper or lower level but again most of hosting providers don’t allow you to override this configuration.  This caused (and causes) many issues for .NET developers with their web applications.  Here I write a short story about this to give my own ideas and solutions.

For those who don’t know about this configuration I would say that this configuration is added to .NET 2.0 by Microsoft to help administrators and developers to deal with their application security easier.  Microsoft has grouped some operations in .NET and give them a level of security.  For example dealing with IO Files and OLEDB connections and several other operations are grouped in Medium trust level.  So once you set your level to Medium, you can’t use these operations in your web applications.  This isn’t fair because some operations are necessary for many applications.  Who can live without IO operations?!  I think Microsoft couldn’t make this configuration clear enough.  Seems that they wanted to help administrators to configure their security easier but this doesn’t mean that they have to set their trust level to lower levels.  Having a High trust level doesn’t mean to have non-secure applications (this is my idea and I say this based on what I got from Microsoft documentations) but unfortunately most of hosters think they should use lower levels to prevent any security issue.

Hosters have to think about security and they have to increase it.  Microsoft added this configuration to help them have a more secure server but there are some techniques to set custom trust levels.

If you don’t know how to set the trust level for your server/application, you should implement this in your machine.config/web.config.  To do this, you can add following line to your configuration file under <system.web> node:

<system.web>

  <trust level="High" originUrl="" />

You can set the level to Full, High, Medium, Low, Minimal.

Also you can do more and lock applications to override this per application in machine.config:

<location allowOverride="false">

  <system.web>

    <trust level="High" originUrl="" />

  </system.web>

</location>

As hosters have done this developers can’t override this configuration in their applications and all the problems bear from here!

But what’s the solution?  How can we find a mean point between hosters and developers?!  This is my own idea based on my experiences but I think this is a good solution:

Hosters can use custom trust level for their servers.  What does this mean?  This means they can enable some operations in a security level.  For example they can enable IO File operations and OLEDB connections for developers and leave other operations disabled in Medium trust level.

To do this and create a custom level based on Medium level, first go to $\%windir%\Microsoft.NET\Framework\v2.0.50727\CONFIG and find a configuration file named web_mediumtrust.config then open and modify it.  Here I modified my configuration to give write access to my C:\Keyvan\Mydir directory.  In this file all of restricted operations are listed and you can change permissions for your needs.

<IPermission

        class="FileIOPermission"

        version="1"

        Read="$AppDir$"

        Write="c:\Keyvan\MyDir;$AppDir$"

        Append="$AppDir$"

        PathDiscovery="$AppDir$"

/>

After making all modifications rename this file to a meaningful name and copy it in the directory where your machine.config is located.  I named my file mycustommediumtrust.config.

Now add this line to your machine.config to enable this custom level of trust:

<system.web>

  <trustLevel name="CustomMedium" policyFile="mycustommediumtrust.config" />

</system.web>

policyFile attribute is the name of modified file (mycustommediumtrust.config for my example).

This will enable some restricted operations on the server.

It’s good to know this is the most common issue for Community Server 2.0 on .NET 2.0.

For more information about Medium trust in ASP.NET 2.0 read this article and to learn how to write your applications based on code access security features in ASP.NET and get the full list of default trust levels and their restrictions read this one on MSDN.

I would get the help of forums and send an email to ask Scott Guthrie to give a short description about this topic on his blog because this is an important issue for me nowadays.

[advertisement] Axosoft OnTime 2008 is four developer tools in one: bug tracking, project wiki, feature management, and help desk. It manages your development process so developers can focus on coding. Installed or Hosted – Free Single-user license -- Free 30-day team trial.

6 Comments : 06.02.06

Feedbacks

 avatar
#1
Vicky
06.09.2006 @ 11:32 AM
Thank you-- this article was very informative and helpful.
 avatar
#2
Sachin Joshi
07.12.2006 @ 12:24 PM
Hi, This is the most annoying of all, my website is using Community Server 2.0 but I am unable to use the Feed Reader and Roller feature due to the Medium Trust Level. As the feed reader needs to use the WebClient or similar functional class to download the RSS xml but the Medium Trust level doesn't allow and throws an security exception. I have tried it in my local as well as production server but was unable to setup the feed reader and roller. Is there anyother way this could be achieved? Regards, Sachin Joshi Visit: www.fuzzydev.com
admin avatar
#3
Keyvan Nayyeri
07.12.2006 @ 1:16 PM
Last night CS team solved this issue and you can use Feed Reader/Roller easily. I heared that you can setup a Proxy server to solve this issue but I haven't applied it myself.
 avatar
#4
Keyvan Nayyeri
08.20.2006 @ 4:10 AM

One of several reasons to add Medium Trust level (any search for a good resource except MSDN points me back ...

 avatar
#5
Faysal
04.20.2007 @ 2:25 PM
I am facing the issue of HIGH LEVEL TRUST all my applications are made in asp.net 2.0 and I have used enterprise library in it and thats why it need high level to run the applications WHAT IS THE POSSIBLE AND QUINKEST SOLUTION
 avatar
#6
Joggee
05.26.2008 @ 8:05 AM

I tried not using data reader, use dataset you dont need to do anything you application will work fine.

for more detail check this out.:

http://blog.joggee.com/?p=168

Leave a Comment