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:


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:

Or when you are running 64 bit:

Inside the SecurityClasses tag, add the following line:

Scroll down and look for the following PermissionSet:

Add the following inside this PermissionSet:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">