.NET 4.0 Medium Trust Level and MYSQL connection

6.1k views Asked by At

I have a problem and I think a proper answer will help a lot of people which is having same problem.Because it is a common and not well answered problem.

The problem is about "Medium trust level" configuration in IIS.A few months ago I coded my website with "Full trust level" configuration on my local machine and when I uploaded it to host that was working fine. But my hosting company have a new rule about trust level,now they only allows "Medium trust level".

My web page doesn't work now.I tried to adapt my website but I have no idea how to do that.And that's sure a lot of people don't know either.. So I will tell what I have done so far, can you help me?

First I tried to changed my local configuration by going here,

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config

and opened my webconfig file,and changed

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

to

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

After that just for testing I created a new website with ASP Default template.It was working.Then I add Mysql.dll as references to connect to Mysql database.But it throwed Security Exception with that stack:

[SecurityException: Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.] MySql.Data.MySqlClient.MySqlTrace..cctor() +0

So what now? Can't we connect to mysql database anymore? Or is there a solution?I never changed my webconfig file in website project folder do I need to change something here? Please help me guys?

2

There are 2 answers

0
Berhan Cem Özelbiçer On BEST ANSWER

Hi everyone I finnally find solution to my problem, so I will try to help that people having same problem.Partial trust and Medium Trust level are same thing note that out.

My first step at the question is absolutely necessary.Don't forget to change your trust level to "Medium". After that you need to get right .Net Connector version from The Oficial Website Be careful to choose version that your hosting provider using. For example my Hosting company is using Mysql Connector Net 6.5.4

Secondly you need to configure your projects webconfig file that will use your .dll files and get the necessary permisson under Medium Trust Level.

You need to add under your <configuration> something like this;

 <mscorlib>
    <security>
      <policy>
        <PolicyLevel version="1">
          <SecurityClasses>
            <SecurityClass Name="MySqlClientPermission" Description="MySql.Data.MySqlClient.MySqlClientPermission, MySql.Data, Version=6.6.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
          </SecurityClasses>
        </PolicyLevel>
      </policy>
    </security>
  </mscorlib>

And if your system doesn't add it automatically under your <system.web> you need to add;

  <assemblies>
    <add assembly="MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    <add assembly="MySql.Data.Entity, Version=6.5.4.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
    <add assembly="MySql.Web, Version=6.5.4.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
  </assemblies>

Sometimes this steps may not be enough and you need to edit your web_mediumtrust.config under the

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config

You need to add this lines under the

<SecurityClasses>
  <securityclass Name="MySqlClientPermission" Description="MySql.Data.MySqlClient.MySqlClientPermission, MySql.Data, Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />

and

<NamedPermissionSets>
  <iPermission class = "MysqlClientPermission" version = "1">  
    <add connectionString = "Server =; Database =;"  restrictions = "User =; User Id =; Password =; Port =; Pooling =;" KeyRestrictionBehavior = "(Prevent Usage | Allow Only)" /> 
  </iPermission>

Be careful about version you are using dont forget to change lines according to that. If still you can't figure that out there is a pretty useful link for you

Trust Level Under the Partial Trust

0
MarzSocks On

Here is how I solved a similar issue with trying to get MySql to work in a medium trust environment.

We tried everything to the ‘book’ and simply could not get it to work!

Turns out the MySql connecter version is the key. Ask you web hosting company what version of the MySqlClient they have installed on their server in GAC.

(GAC simply means the library is installed on the server so that it can be referenced directly and without you having to upload the MySql.Data.dll to your bin folder. The latest installable version is available here: http://dev.mysql.com/downloads/connector/net/).

Once the driver is installed on the server in GAC you just need to reference it in your web.config like this, but replace Version=6.6.5.0 with your web hosting companies version:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="MySql.Data, Version=6.6.5.0, Culture=neutral, PublicKeyToken=C5687FC88969C44D"/>
  </assemblies>    
</compilation>