Microsoft Internet Information Services (IIS) logo

MySQL Connector/NET 6.5 in partial trust

How to use MySQL Connector/NET 6.5 with MySqlClientPermission Class in partial (medium) trust

Home » Windows Server » MySQL Connector/NET 6.5 in partial trust

MySQL Connector/NET 6.5 introduces a new MySqlClientPermission class in trust policies. This class can be used to restrict data/MySQL access.

Using MySQL Connector/NET 6.5 with MySqlClientPermission Class in partial (medium) trust

To prevent Security Exceptions, this SecurityClass and IPermission must be configured in the web_mediumtrust.config file.

Open the file (C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web_mediumtrust.config) and add:

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

<!-- [...] -->

<iPermission class="MySqlClientPermission"
  version="1">
  <add connectionString="Server=;Database=;"
    restrictions="User=;User Id=;Password=;Port=;Pooling=;"
    KeyRestrictionBehavior="(PreventUsage|AllowOnly)" />
</iPermission>

Test the configuration with the following C# script:

<%@ Page Language="C#" %>
<%@ Import Namespace="MySql.Data" %>
<%@ Import Namespace="MySql.Data.MySqlClient" %>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
  MySQLConn();
}
void MySQLConn()
{
  string connStr = "Server=mysql_host;User=mysql_user;" +
    "database=mysql_db;password=mysql_passwd;";
  MySqlConnection conn = new MySqlConnection(connStr);
  try
  {
    Response.Write("Connecting to MySQL... ");
    conn.Open();
    string sql = "SELECT * FROM table_name";
    MySqlCommand cmd = new MySqlCommand(sql, conn);
    MySqlDataReader rdr = cmd.ExecuteReader();
    while (rdr.Read())
    {
      Response.Write(rdr[0]+ " " +rdr[1]);
    }
    rdr.Close();
  }
  catch (Exception ex)
  {
    Response.Write(ex.ToString());
  }
  conn.Close();
  Response.Write("Done.");
}
</script>

Did you like this post?

Your generosity helps pay for the ongoing costs associated with running this website like coffee, hosting services, library mirrors, domain renewals, time for article research, and coffee, just to name a few.

See also  Detect ethernet network speed in Windows
0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
MySQL Connector/NET and Entity Framework - Sysadmins of the North
2025-11-18 9:42 am

[…] MySQL Connector/NET and Entity Framework. You can use the website configuration from my article “MySQL Connector/NET 6.5 in partial trust” to set up MySQL Connector/NET for your website application. There are a few other settings you […]

trackback
How-to: limited Socket.Net.SocketPermission in ASP.NET 2.0 Medium Trust - Sysadmins of the North
2025-11-18 10:38 am

[…] MySQL Connector/NET 6.5 in partial trust […]