Quantcast
Viewing all articles
Browse latest Browse all 18769

Running Connector/Net 6.5 inside Medium-Trust Level

As you probably know, there were some problems using our connector in medium-trust level scenarios. Most hosting services provide MySQL access.  Most of these hosting providers run their .NET web apps using medium trust.  Prior to 6.5, our connector required the hosting provider to either run the application in full trust or to enable broad privileges like SocketPermission globally.  Many hosting providers are unwilling to do that.  So fully enabling our provider to run in a partial trust scenario was a strongly requested feature. The request was a very simple task: enable Connector/Net to work correctly in a medium-trust level environment when the library is installed in the GAC.

The implementation consisted of including the necessary security imperative asserts so the CLR allows our code to perform the operations where it needs permission to perform.

The permissions that we needed were:

- System.Net.SocketPermission

- System.Security.Permissions.ReflectionPermission

- System.Net.DnsPermission 

- System.Security.Permissions.SecurityPermission 

Starting from 6.5 you can use the Connector/Net library inside any medium-trust level environment with out any issue.  You should note that the hosting provider will need to install our library in the GAC however they can avoid granting permissions globally by using the new MySqlClientPermission class in the trust policies.

Let's put some code together to see it working.

For this little application you need to have  MySQL server up an running and enable it to use pipe connections. To do so you need to add the -enable-named-pipe option on the command line. (If you need more information about this please see http://dev.mysql.com/doc/refman/5.5/en/windows-installation.html).

 1 - Create a simple web application using VS 2010

 2 - Add the reference in your application for our library. 

 3 - Edit your web.config file so your application run using a Medium trust level.

<system.web>

    <trust level="Medium"/>

 </system.web>

 4.  Add the MySql.Data.MySqlClient namespace to your server-code page.

 5. Define the connection string:

MySqlConnectionStringBuilder myconnString = new MySqlConnectionStringBuilder("server=localhost;User Id=root;database=test;"   );

          myconnString.PipeName = "MySQL55";

          myconnString.ConnectionProtocol = MySqlConnectionProtocol.Pipe;

6. Define the MySqlConnection to use:

          MySqlConnection myconn = new MySqlConnection(myconnString.ConnectionString);

          myconn.Open();

7. 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()

 8. Execute!!

Noticed that you don't need to add any special code so your application can run properly inside Medium-Trust. 

Now you should be able to see your application running with out any security problems.

Please feel free to ask all your questions related to this new feature or ask for more information if you need so.

I hope you have found this information useful. 

Happy MySQL/Net Codding! Image may be NSFW.
Clik here to view.
:)

Some useful references related:

Connection to MySql Server  (http://dev.mysql.com/doc/refman/5.1/en/connecting.html)

Windows Authentication (http://blogs.oracle.com/mysql_wna_plugin/entry/windows_native_authentication_for_mysql)


PlanetMySQL Voting: Vote UP / Vote DOWN

Viewing all articles
Browse latest Browse all 18769

Trending Articles