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 four 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.
4. Recompile the MySQL library to
run under medium trust?
1. Download the source code for the MySQL Connector/Net from www.mysql.com.
2. Extract the contents of the zip file to a local directory.
3. Open mysql.csproj project file in Visual Studio.
4. Open the AssemblyInfo.cs file, and add the following code, in the using block, at the top of the file (if it is not already there):
using System.Security;
6. Add the following code to the assembly section of the file:
[assembly: AllowPartiallyTrustedCallers]
8. Recompile the dll.
Njoy Coding...
Njoy Coding...