marthijn. Rotating Header Image

Fix .NET 4 SocketPermission for MySQL driver in Medium Trust

Most .NET 4 shared hosting providers offering their customers a medium trust environment. This is not a problem for most web applications unless the applications uses MySQL in combination with the .NET MySQL driver (MySQL.Data.dll). The MySQL driver connects to the MySQL database using a socket. However, sockets are not allowed in medium trust. The application will throw a SecurityException with the following message:

Request for the permission of type 'System.Net.SocketPermission,  System, Version=4.0.0.0, Culture=neutral,  PublicKeyToken=b77a5c561934e089' failed.


In order to fix this there are three solutions:

1. Instead of MySQL, use Microsoft SQL server.

2. Move to a hosting provider that supports a full trust environment or get your own server.

3. Ask your hosting provider to update the medium trust policy with SocketPermission. The steps will be explained here:

Open the following file in a text editor:

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

Or when you are running 64 bit:

C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web_mediumtrust.config

Inside the SecurityClasses tag, add the following line:

<SecurityClass Name="SocketPermission" Description="System.Net.SocketPermission, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

Scroll down and look for the following PermissionSet:

<PermissionSet version="1" Name="ASP.Net">

Add the following inside this PermissionSet:

<IPermission class="SocketPermission" version="1" Unrestricted="true" />

That’s all, a restart of IIS is, as far as I know, not necessary.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply