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>
Also read:

Did you like this post?
Please take a second to support Sysadmins of the North and donate! ❤️
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. ❤️





