.NET applications operate under a given trust level. Normal desktop applications operate under full trust, while web applications that are hosted in shared environments are normally run under the partial trust level (also known as “medium trust”). Some hosting providers host shared applications in their own app pools and allow the application to run under full trust, but this configuration is relatively rare. The Connector/Net support for partial trust has improved over time to simplify the configuration and deployment process for hosting providers.
Connector/Net 6.5 fully enables our provider to run in a partial
trust environment when the library is installed in the Global
Assembly Cache (GAC). The new
MySqlClientPermission class, derived from the
.NET DBDataPermission class, helps to simplify
the permission setup.
Starting from 6.5 you can use the Connector/Net library inside any medium-trust level environment without any issue.
The following list shows steps and code fragments needed to run a Connector/Net application in a partial trust environment. For illustration purposes, we use the Pipe Connections protocol in this example.
Configure the MySQL server to accept pipe connections, by
adding the --enable-named-pipe option on
the command line. If you need more information about this, see
Section 2.3, “Installing MySQL on Microsoft Windows”.
Confirm that the hosting provider has installed the
Connector/Net library (MySql.Data.dll) in
the GAC.
Optionally, the hosting provider can avoid granting
permissions globally by using the new
MySqlClientPermission class in the trust
policies. (The alternative is to globally enable the
permissions System.Net.SocketPermission,
System.Security.Permissions.ReflectionPermission,
System.Net.DnsPermission, and
System.Security.Permissions.SecurityPermission.)
Create a simple web application using Visual Studio 2010.
Add the reference in your application for the
MySql.Data.MySqlClient library.
Edit your web.config file so that your
application runs using a Medium trust level:
<system.web>
<trust level="Medium"/>
</system.web>
Add the MySql.Data.MySqlClient namespace to
your server-code page.
Define the connection string:
MySqlConnectionStringBuilder myconnString = new MySqlConnectionStringBuilder("server=localhost;User Id=root;database=test;" );
myconnString.PipeName = "MySQL55";
myconnString.ConnectionProtocol = MySqlConnectionProtocol.Pipe;
Define the MySqlConnection to use:
MySqlConnection myconn = new MySqlConnection(myconnString.ConnectionString); myconn.Open();
Retrieve some data from your tables:
MySqlCommand cmd = new MySqlCommand("Select * from products", myconn);
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataSet1 tds = new DataSet1();
da.Fill(tds, tds.Tables[0].TableName);
GridView1.DataSource = tds;
GridView1.DataBind();
myconn.Close()
Run the program. It should execute successfully, without requiring any special code or encountering any security problems.
Starting with these versions, Connector/Net can be used under
partial trust hosting that has been modified to allow the use of
sockets for communication. By default, partial trust does not
include SocketPermission. Connector/Net uses
sockets to talk with the MySQL server so the hosting provider must
create a new trust level that is an exact clone of partial trust
but that has the following permissions added:
System.Net.SocketPermission
System.Security.Permissions.ReflectionPermission
System.Net.DnsPermission
System.Security.Permissions.SecurityPermission
Connector/Net versions prior to 5.0.8 and 5.1.3 were not compatible with partial trust hosting.

User Comments
Add your own comment.