<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Nathan Bridgewater &#187; MySql</title>
	<atom:link href="http://www.integratedwebsystems.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.integratedwebsystems.com</link>
	<description>My Little .NET Sandbox</description>
	<lastBuildDate>Thu, 19 Jan 2012 20:37:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setting up Mono 2.8 with Asp.Net 4.0 and MVC2 on Ubuntu with MySql Membership</title>
		<link>http://www.integratedwebsystems.com/2010/11/setting-up-mono-2-8-with-asp-net-4-0-and-mvc2-on-ubuntu-with-mysql-membership/</link>
		<comments>http://www.integratedwebsystems.com/2010/11/setting-up-mono-2-8-with-asp-net-4-0-and-mvc2-on-ubuntu-with-mysql-membership/#comments</comments>
		<pubDate>Tue, 23 Nov 2010 06:00:00 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=1024</guid>
		<description><![CDATA[I’ve had a few requests to write a full walk-through installing Mono...]]></description>
			<content:encoded><![CDATA[<p>I’ve had a few requests to write a full walk-through installing <a href="http://mono-project.com">Mono 2.8</a> w/ Asp.Net 4.0 since I posted an <a href="/2010/10/mono-2-8-install-script-for-ubuntu-fedora/">install script for Mono 2.8 on Ubuntu and Fedora</a>.  So here it is! We’re going to be setting up a new server from scratch then installing just the basics of Mono 2.8 (Mono, GTK, GDI, XSP, &amp; Mod_Mono). Then we’ll use that to setup a new Apache server with a basic Asp.Net 4.0 MVC2 application w/ MySql Membership.  That sounds like a lot, but it’s really very straight forward. So lets get started!</p>
<p><span id="more-1024"></span></p>
<h4>Update! 1/30/2011</h4>
<p>Mono 2.8.2 has become available since this article was posted and is highly recommended due to security enhancments. Refer to <a href="/2011/01/mono-2-8-2-script-updated-for-ubuntu-and-fedora/">install script for Mono 2.8.2</a> instead of 2.8. Pay close attention to your virtual host config so you use the correct mono install prefixes. Check MonoServerPath and MonoSetEnv directives below.</p>
<h2>Setup Your Server</h2>
<p>For our server, we’re going to setup a VM of 32 bit <a href="http://www.ubuntu.com/server/get-ubuntu/download">Ubuntu Server</a> 10.04 LTS using <a href="http://www.virtualbox.org/wiki/Downloads">VirtualBox</a>. You can use whichever VM software you want or setup a physical server. This is just an easy sandbox system for this walkthrough. I noticed VirtualBox requires hardware virtualization for 64bit guest systems; keep that in mind if you test with a 64bit server.</p>
<p>I applied default configuration settings to my VM with 512 RAM and a bridged network interface so I could communicate with it directly from another machine. After that, I mounted the Ubuntu Server install ISO to the disc drive and booted it into installation.  During installation, the only package option I enabled was the OpenSSH Server (use arrows, spacebar, and tabs to move around the installer).  I’m not going to worry about other things like a web server since the Mono installer script will do that for us. Once you’ve completed the installer, unmount the ISO, boot up, and login. It defaults to using DHCP for ethernet, so find the current IP using:</p>
<pre class="brush: bash; gutter: false;">ifconfig eth0</pre>
<p>Using the IP Address listed here, connect to the box with an SSH client of your choice (I tend to use <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html">Putty</a> or the OpenSSH client built into <a href="http://code.google.com/p/msysgit/downloads/list">msysGit</a> on Windows).  Immediately update your server. If you haven’t used “sudo” before, it will request your sudo password, which is just your normal account password you setup during install.</p>
<pre class="brush: bash; gutter: false;">ssh nathan@ubuntu
sudo apt-get update
sudo apt-get dist-upgrade
sudo apt-get upgrade -y</pre>
<p>&nbsp;</p>
<h2>Install Mono</h2>
<p>Once you’re updated, we can download the install script and install Mono, Apache, and dependencies.</p>
<pre class="brush: bash; gutter: false;">wget --no-check-certificate https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.8.shchmod 755 install_mono-2.8.sh./install_mono-2.8.sh</pre>
<p>This will re-run another update and then download, compile, and install everything required to run Mono 2.8. It will also compile and install other Mono-related items like: GTK, GDIPlus, XSP, and Mod_mono. <del>Once it completes, update your system path and reboot.</del> <del>Edit “/etc/environment” and insert “<em>/opt/mono-2.8/bin</em>:” to the beginning of the PATH value.</del></p>
<p><del></del>Once completed, refer to this more recent post about setting up MVC, which also includes information about setting up your <a href="/2011/06/get-mvc3-razor-running-on-mono/">environment for the new install</a>. You can test it with “mono –V”.</p>
<pre class="brush: bash; gutter: false;">nathan@ubuntu:~$ mono -V
Mono JIT compiler version 2.8 (tarball Mon Nov 15 14:39:28 CST 2010)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  x86
        Disabled:      none
        Misc:          debugger softdebug
        LLVM:          supported, not enabled.
        GC:            Included Boehm (with typed GC and Parallel Mark)
nathan@ubuntu:~$</pre>
<p>&nbsp;</p>
<p>So now we just need to configure Apache. The install process copied a mod_mono.conf to the directory: /etc/apache2.  You’ll need to enable this by moving it to /etc/apache2/mods-available and creating a link to that file at /etc/apache2/mods-enabled.</p>
<pre class="brush: bash; gutter: false;">cd /etc/apache2
sudo mv mod_mono.conf mods-avail*
sudo ln -s /etc/apache2/mods-available/mod_mono.conf /etc/apache2/mods-enabled/mono.conf

#optionally restart apache server. more changes coming
sudo service apache2 restart</pre>
<p>&nbsp;</p>
<h2>Configure Website</h2>
<p>We’re using <a href="http://mono-project.com/Mod_mono">Mod_Mono</a> to handle Asp.Net requests with Apache. I’ve mentioned before that I’m a big fan of the Mono website’s <a href="http://go-mono.com/config-mod-mono/">Mod_Mono configuration tool</a>. We can use that tool to build our initial VirtualHost configuration. Then change the MonoServerPath to be the one we installed at: “/opt/mono-2.8/bin/mod-mono-server4”  When you’re done, upload (or move) this folder to your home directory on the server.</p>
<p><em>NOTE: We’re using mod-mono-server<strong>4 </strong>for Asp.Net 4.0 runtime. You can use mod-mono-server2 for 2.0-3.5 Asp.Net and mod-mono-server for 1.0-1.1 Asp.Net. </em></p>
<p><em>NOTE: If you’re unfamiliar with copying files from your Windows box to a Linux one, you can use </em><a href="http://filezilla-project.org/download.php"><em>FileZilla</em></a><em> with an SSH (SFTP) connection. Connect to something like: “sftp://username@host” then treat it like any other FTP connection. </em></p>
<p>Here we’ll setup our web folders, permissions, move our configuration file, link it, and restart the web server. Our generated configuration file is named <em>ubuntu.conf</em> and was uploaded to my home folder.</p>
<pre class="brush: bash; gutter: false;">sudo mkdir /srv/www
sudo mkdir /srv/www/ubuntu
sudo chown root:www-data /srv/www/ubuntu -R
sudo chmod 775 /srv/www/ubuntu -R
mv ~/ubuntu.conf /etc/apache2/sites-available
cd /etc/apache2/sites-en*
sudo ln -s ../sites-available/ubuntu.conf "000-ubuntu.conf"
sudo service apache2 restart</pre>
<p><em>NOTE: I prefixed the link name with “000” since Apache loads configuration files in alphabetical order. It’s not necessary for this scenario, but if you’re running multiple virtual hosts and want a “default”, this is good to know. Make your default VirtualHost load first.</em></p>
<p>You might also want to give yourself permissions to write to the new web directories for publishing sites. The easy way to do this (maybe not best practice), would be to add yourself to the www-data group used by apache &amp; mono, which we just setup with full access.</p>
<pre class="brush: bash; gutter: false;">sudo usermod -a -G www-data your_username</pre>
<p>Later when we upload the site, this will be a factor. The alternative will be to upload your published site to your home directory and then with an ssh console, <em>sudo mv</em> the published files to the web directories if you choose not to add yourself to the www-data group.</p>
<p>For this sample, we used all the default parameters in our configuration. Take note that debugging is enabled. I also left IO Mapping enabled to allow Mono to treat file system paths as case insensitive. You can improve performance by disabling IO Mapping. The VirtualHost configuration looks like this:</p>
<pre class="brush: xml; gutter: false;">&lt;VirtualHost *:80&gt;
  ServerName ubuntu
  ServerAdmin web-admin@ubuntu
  DocumentRoot /srv/www/ubuntu
  # MonoServerPath can be changed to specify which version of ASP.NET is hosted
  # mod-mono-server1 = ASP.NET 1.1 / mod-mono-server2 = ASP.NET 2.0
  # For SUSE Linux Enterprise Mono Extension, uncomment the line below:
  # MonoServerPath ubuntu "/opt/novell/mono/bin/mod-mono-server2"
  # For Mono on openSUSE, uncomment the line below instead:
  MonoServerPath ubuntu "/opt/mono-2.8/bin/mod-mono-server4"

  # To obtain line numbers in stack traces you need to do two things:
  # 1) Enable Debug code generation in your page by using the Debug="true"
  #    page directive, or by setting &lt;compilation debug="true" /&gt; in the
  #    application's Web.config
  # 2) Uncomment the MonoDebug true directive below to enable mod_mono debugging
  MonoDebug ubuntu true

  # The MONO_IOMAP environment variable can be configured to provide platform abstraction
  # for file access in Linux.  Valid values for MONO_IOMAP are:
  #    case
  #    drive
  #    all
  # Uncomment the line below to alter file access behavior for the configured application
  MonoSetEnv ubuntu MONO_IOMAP=all;LD_LIBRARY_PATH=/opt/mono-2.8/lib:$LD_LIBRARY_PATH;PATH=/opt/mono-2.8/bin:$PATH
  #
  # Additional environtment variables can be set for this server instance using
  # the MonoSetEnv directive.  MonoSetEnv takes a string of 'name=value' pairs
  # separated by semicolons.  For instance, to enable platform abstraction *and*
  # use Mono's old regular expression interpreter (which is slower, but has a
  # shorter setup time), uncomment the line below instead:
  # MonoSetEnv ubuntu MONO_IOMAP=all;MONO_OLD_RX=1

  MonoApplications ubuntu "/:/srv/www/ubuntu"
  &lt;Location "/"&gt;
    Allow from all
    Order allow,deny
    MonoSetServerAlias ubuntu
    SetHandler mono
    SetOutputFilter DEFLATE
    SetEnvIfNoCase Request_URI "\.(?:gif|jpe?g|png)$" no-gzip dont-vary
  &lt;/Location&gt;
  &lt;IfModule mod_deflate.c&gt;
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript
  &lt;/IfModule&gt;
&lt;/VirtualHost&gt;</pre>
<p>&nbsp;</p>
<h2>Create and Publish Asp.Net MVC2 Application</h2>
<p>In Visual Studio 2010, select the File menu –&gt; New –&gt; Project. In the new project dialog near the top center, select the .NET Framework 4.  Then on the left, select Web (under C#), and choose ASP.NET MVC2 Web Application.  There are a few key items we’ll need to remove to make this compatible with Mono.</p>
<p>First, open the <em>~/Models/AccountModels.cs</em> file and remove all data annotations attributes from the three model classes’ properties. You may leave “DisplayName”. I’ve found (without investing a lot of time in this), that the data annotations doesn’t seem to work out of the box. Removing these attributes will allow it to run properly under Mono. Keep in mind, this also means, you’ll need to write your own server-side validation. It’s not too painful; we’ve been doing this for  years anyway.</p>
<p>Finally, build and test-run the site from visual studio. It should work fine with the built-in Casini test web server. If all is well, right click on the project (in solution explorer) and Publish the site. Publish it to the file system and choose a destination folder. You don’t need to include the App_Data folder, and you can delete all files before publish if you want.</p>
<p>Upload the published files to the server using FileZilla, and copy them to <em>/srv/www/ubuntu</em>.  Apache doesn’t need to be restarted when you update web files and assemblies. You should now be able to browse the site at <em>http://your_test_server_host/</em>.  All should work except membership since we haven’t configured the database yet.</p>
<p>&nbsp;</p>
<h2>Setup Membership with MySql</h2>
<p>Awhile back, I wrote a <a href="/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/">post dedicated to MySql Membership</a> with a simple <a href="/tools-source/">config generation tool</a> do this for you. Checkout the post for more details after setting up your database server. Setting up membership is identical between Asp.Net MVC and web forms. Install and configure MySql Server first. Then setup a placeholder database for the membership data. The first time you use membership during runtime will automatically generate the database schema.</p>
<h3>Install MySql and Setup Empty Membership Database</h3>
<pre class="brush: bash; gutter: false;">sudo apt-get install mysql-server</pre>
<p><em>NOTE: The default install will lock it down to localhost access only, which is good for a server. But for test, if you want to access it from your machine, you’ll have to tell it to bind to a LAN address (instead of 127.0.0.1). Take a peek at /etc/msyql/my.cnf file to update this setting. The comments describe the change.</em></p>
<p>I’m giving application user full access (including schema change privileges) to the membership database. MySql Memebrship provider updates its own schema as you update the provider.  Checkout the <a href="http://dev.mysql.com/doc/">MySql docs</a> if you want to limit account privileges.</p>
<pre class="brush: bash; gutter: false;">nathan@ubuntu:~$ mysql -h localhost -u root -p
mysql: create database membership;
mysql: grant all privileges on membership.* to 'aspnet_user'@'localhost' identified by 'secret_password';
mysql: quit</pre>
<h3>Configure Membership</h3>
<p>I used my configuration tool and generated a simple membership config.  Copy the membership sections to your into the “system.web” section of your root web.config and setup a connection string with the same name as the one used in membership. (“Default” is used here).</p>
<p>Next, if you’re using a factory default version of MySql Connector/NET, you’ll need to download its source and recompile it with .NET 4.0 profile. I found while doing this that the published 2.0 version seemed to blow up when referencing transaction methods. I found that it was a known bug and should be resolved in upcoming versions of the connector. For now, you’ll need to recompile MySql.Web and MySql.Data with a 4.0 profile if you want it to work under Mono with membership.  The sample version packaged here has that done already.</p>
<p><em>NOTE: The version of MySql .NET Connector I was using at the time I wrote this tool (6.3.5) didn’t support hashed passwords with Mono runtime. I made a minor tweak to this version that will disable the Mono runtime check for hashed passwords and will allow it (since it does work in the current version of Mono).  </em></p>
<pre class="brush: xml; gutter: false;">&lt;authentication mode="Forms"&gt;
    &lt;forms
      loginUrl="~/Account/Logon"
      timeout="30"
      name="6ad58f32-93d5-463f-9d99-835942aca8f5"
      path="/"
      requireSSL="false"
      slidingExpiration="true"
      defaultUrl="Default.aspx"
      enableCrossAppRedirects="false"/&gt;
&lt;/authentication&gt;

&lt;membership defaultProvider="MySqlMembershipProvider"&gt;
    &lt;providers&gt;
        &lt;clear/&gt;
        &lt;add name="MySqlMembershipProvider"
              type="MySql.Web.Security.MySQLMembershipProvider, mysql.web"
                connectionStringName="Default"
                enablePasswordRetrieval="false"
                enablePasswordReset="true"
                requiresQuestionAndAnswer="false"
                requiresUniqueEmail="true"
                passwordFormat="hashed"
                maxInvalidPasswordAttempts="5"
                minRequiredPasswordLength="6"
                minRequiredNonalphanumericCharacters="0"
                passwordAttemptWindow="10"
                applicationName="/"
                autogenerateschema="true"/&gt;
    &lt;/providers&gt;
&lt;/membership&gt;

&lt;roleManager enabled="true" defaultProvider="MySqlRoleProvider"&gt;
    &lt;providers&gt;
        &lt;clear /&gt;
        &lt;add connectionStringName="Default"
            applicationName="/"
            name="MySqlRoleProvider"
            type="MySql.Web.Security.MySQLRoleProvider, mysql.web"
            autogenerateschema="true"/&gt;
    &lt;/providers&gt;
&lt;/roleManager&gt;

&lt;profile&gt;
    &lt;providers&gt;
        &lt;clear/&gt;
        &lt;add type="MySql.Web.Security.MySqlProfileProvider, mysql.web"
              name="MySqlProfileProvider"
              applicationName="/"
              connectionStringName="Default"
              autogenerateschema="true"/&gt;
    &lt;/providers&gt;
&lt;/profile&gt;</pre>
<p>&nbsp;</p>
<p>Also for Mono, I added the DbProviderFactories section for MySql data in case it wasn’t already setup. Insert this adjacent to the <em>&lt;system.web&gt;</em> section.</p>
<pre class="brush: xml; gutter: false;">&lt;system.data&gt;
    &lt;DbProviderFactories&gt;
        &lt;clear/&gt;
        &lt;add name="MySQL Data Provider"
            description="ADO.Net driver for MySQL"
            invariant="MySql.Data.MySqlClient"
            type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data"/&gt;
    &lt;/DbProviderFactories&gt;
&lt;/system.data&gt;</pre>
<p>Once updated, you should now be able to run the MVC app and register/authenticate user accounts. If you have any problems, double-check your data source value, username and password. Make sure you can connect to it with a mysql client and your app credentials.  Also I find the Asp.Net Configuration Tool built-in to Visual Studio helpful when testing Membership configuration. It will show you a number of users and roles if membership is configured properly.</p>
<p>&nbsp;</p>
<h2>Sample</h2>
<p>Grab the sample from <a href="/resources/p1024/sample-asp4-mvc2.zip">here</a> or <a href="https://github.com/nathanb/iws-snippets/tree/master/sample-asp4-mvc2/">github</a>. Includes: Web App, dependencies, &amp; virtual host config. Comments, enhancements, pull requests, are welcome!</p>
<p>&nbsp;</p>
<h2>Wrap-Up</h2>
<p>So now we’re done. The next thing you should checkout is how to use <a href="http://msdn.microsoft.com/en-us/library/aa973811.aspx">inversion of control</a> with MVC2. I’ve been writing applications lately using <a href="http://www.subsonicproject.com/">SubSonic3</a> (<a href="http://subsonicproject.com/docs/Using_SimpleRepository">repository mode</a>) and <a href="http://unity.codeplex.com/">Unity Application Block 2.0</a> for dependency injection. Coupled with <a href="http://mvccontrib.codeplex.com/">MvcContrib</a> test helpers and <a href="http://www.ayende.com/projects/rhino-mocks.aspx">Rhino Mocks</a>, this is a powerful combination for writing rock solid, testable applications that run on Windows AND Mono.</p>
<p>Here are a few useful links for this post:</p>
<ul>
<li><a href="https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/ubuntu/install_mono-2.8.sh">Install script for Mono 2.8 on Ubuntu</a></li>
<li><a href="https://github.com/nathanb/iws-snippets/raw/master/mono-install-scripts/fedora/install_mono-2.8.sh">Install script for Mono 2.8 on Fedora</a></li>
<li><a href="/resources/p1024/sample-asp4-mvc2.zip">Sample application built here</a>. Includes: Web App, dependencies, &amp; virtual host config.</li>
<li><a href="http://dev.mysql.com/downloads/connector/net/">MySql .NET Connectors w/ Data Provider and Membership Provider</a></li>
<li><a href="/tools-source/">MySql membership configuration generation tool</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2010/11/setting-up-mono-2-8-with-asp-net-4-0-and-mvc2-on-ubuntu-with-mysql-membership/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>Walkthrough: Porting Asp.Net MVC Website to Mono 2.6.1 and MySql on Linux Apache &#8211; Porting to Mono Part 3 of 3</title>
		<link>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/</link>
		<comments>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 04:21:00 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[Mono]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=447</guid>
		<description><![CDATA[So for the third and final part of this series, I want...]]></description>
			<content:encoded><![CDATA[<p sizcache="0" sizset="0">So for the third and final part of this series, I want to walk through how I ported a very small project I posted on CodePlex awhile back called <a href="http://sitemanager.codeplex.com" target="_blank">SiteManager</a>. It is an extremely simplified CMS application that I wrote using Linq to Sql and MVC in order to provide a very basic and easy website tool. It uses membership provider for authentication and Linq to Sql Classes with its database in MS Sql Server. For this walkthrough, I’ll be converting the database to MySql and adapting Linq to Sql to Dblinq so it can completely run on Mono and Linux.</p>
</p>
<p> <span id="more-447"></span>
<link rel="stylesheet" type="text/css" href="/css/jquery.lightbox-0.5.css" />
<h3>Index</h3>
<ul sizcache="0" sizset="1">
<li sizcache="0" sizset="1"><a href="#start">Getting Started</a> </li>
<li sizcache="0" sizset="2"><a href="#database">Database Migration</a> </li>
<li sizcache="0" sizset="3"><a href="#l2s">Linq to Sql to Dblinq</a> </li>
<li sizcache="0" sizset="4"><a href="#membership">Membership</a> </li>
<li sizcache="0" sizset="5"><a href="#authorization">Authorization</a> </li>
<li sizcache="0" sizset="6"><a href="#deployment">Deployment</a> </li>
<li sizcache="0" sizset="7"><a href="#vim">VIM (Editing code files on the server)</a> </li>
<li sizcache="0" sizset="8"><a href="#wrapup">Wrap Up</a> </li>
<li sizcache="0" sizset="9"><a href="#source">Get the Source</a> </li>
</ul>
<h3 sizcache="0" sizset="10"><a name="start">Getting Started</a></h3>
<p sizcache="0" sizset="11">Since I’ve already ported this application, it is very easy to identify any of the problem points. But for any new project, I would identify the basic items related to common issues with changing platforms like: data access, authentication, and any operating system specific code. Using the <a href="http://mono-project.com/MoMA" target="_blank">MoMA tool</a> is hugely beneficial to see how your code base might fare in the Mono framework. It will point out any specific code you’re using that could cause problems.&#160;&#160; You should also <a href="http://mono-project.com/Start" target="_blank">browse the Mono site</a> and learn some basics about the framework since they have quite a few useful extended frameworks that are not part of the Windows .Net runtime.&#160; They also have another nice guide out there for <a href="http://www.mono-project.com/Guide:_Porting_ASP.NET_Applications">porting Asp.Net applications</a> where they use PostgreSql and Blog Starter Kit.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="14"><a name="database">Database Migration</a></h3>
<p>You should know that Microsoft Sql Server works fine with Mono and works well when you are in a Windows environment and want to run Mono applications against existing Sql Servers. Since we’re running this application completely on a Linux server, as you might do in a hosted Linux environment, leaving our database in Sql Server isn’t an option.&#160; I chose to migrate to MySql purely for personal preference since I have a little more experience with this one rather than others like PostgreSql or Sqlite. However with Dblinq, you will have quite a few options.</p>
<h4>Create an Empty MySql Database and an Application User</h4>
<p sizcache="0" sizset="15">If you recall from my first post in this series, we <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/#InstallMonoAndApache">installed and configured MySql server</a>.&#160;&#160; We also discussed creating new users. You will want to create a new database with a new user who has access to it from the local machine. (Note that this can also be performed with the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Workbench GUI Tools</a>).</p>
<p>So to begin, using Putty, SSH into your server.&#160; Then run:</p>
<pre class="brush: sql; gutter: false;">root@server: mysql -u admin_username -p
Enter password: ********
create database sitemanager_mono;
grant all privileges on sitemanager_mono.* to 'aspnet'@'localhost' identified by 'pass';
quit</pre>
<p>You may want to replace the username and password I used here with something more secure, but for this demo I kept it simple. You can do that by replacing ‘aspnet’ with a username of your choice and ‘pass’ with a password of your choice.</p>
<p>You now have a new empty database with its own power user account that can only access it from the local machine. This will be our application user so keep the credentials handy for later when we edit the config file.</p>
<h4>Migrating Sql Server Schema to MySql</h4>
<p sizcache="0" sizset="17">I used the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Migration Toolkit</a> (part of the GUI Workbench) to perform my migration. It actually worked out pretty well with the exception of a few data type conversions. I like to use varchar(max) fields in Sql server. I also use bit fields for booleans since they parse right over to C# booleans.&#160; The migration tool didnt’ like varchar(max) and converted them to varchar(-1). It also converted my bit fields to tinyint(4). So during the migration, I was able to make edits to a few of my tables that contained those fields. For varchar(max), you have a choice. You can: use a smaller field like varchar(5000), use a Text field, or you can choose to store your text data in a binary blob and encode/decode it to UTF8.&#160; For this, application, I went the route of selecting a smaller varchar field for simplicity; but if my content pages were large, I would probably go the route of text. I found some <a href="http://www.pythian.com/news/7129/text-vs-varchar/" target="_blank">interesting discussion in this post on the topic.</a></p>
<p>So in order, here are the steps I took using the migration tool:</p>
<table border="0" cellspacing="0" cellpadding="2" width="561" sizcache="0" sizset="19">
<tbody sizcache="0" sizset="19">
<tr sizcache="0" sizset="19">
<td valign="top" width="263" sizcache="0" sizset="19"><a class="lightbox" title="Step 1" href="/resources/p447/1.jpg" jquery1267071925472="12" jquery1267072108070="12"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 1" border="0" alt="Step 1" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/1.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">&#160;</td>
</tr>
<tr sizcache="0" sizset="20">
<td valign="top" width="263" sizcache="0" sizset="20"><a class="lightbox" title="Step 2" href="/resources/p447/2.jpg" jquery1267071925472="14" jquery1267072108070="14"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 2" border="0" alt="Step 2" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/2.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Choose your source database.</p>
</td>
</tr>
<tr sizcache="0" sizset="21">
<td valign="top" width="263" sizcache="0" sizset="21"><a class="lightbox" title="Step 3" href="/resources/p447/3.jpg" jquery1267071925472="16" jquery1267072108070="16"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 3" border="0" alt="Step 3" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/3.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Choose your target database.</p>
</td>
</tr>
<tr sizcache="0" sizset="22">
<td valign="top" width="263" sizcache="0" sizset="22"><a class="lightbox" title="Step 4" href="/resources/p447/4.jpg" jquery1267071925472="18" jquery1267072108070="18"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 4" border="0" alt="Step 4" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/4.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Select the source schema. </td>
</tr>
<tr sizcache="0" sizset="23">
<td valign="top" width="263" sizcache="0" sizset="23"><a class="lightbox" title="Step 5" href="/resources/p447/5.jpg" jquery1267071925472="20" jquery1267072108070="20"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="" border="0" alt="" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/5.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Add exclusions. I chose to exclude my membership tables since I configured those to use a different catalog. </td>
</tr>
<tr sizcache="0" sizset="24">
<td valign="top" width="263" sizcache="0" sizset="24"><a class="lightbox" title="Step 6" href="/resources/p447/6.jpg" jquery1267071925472="22" jquery1267072108070="22"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 6" border="0" alt="Step 6" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/6.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Utf-8 was fine for my solution</td>
</tr>
<tr sizcache="0" sizset="25">
<td valign="top" width="263" sizcache="0" sizset="25"><a class="lightbox" title="Step 7" href="/resources/p447/7.jpg" jquery1267071925472="24" jquery1267072108070="24"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 7" border="0" alt="Step 7" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/7.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Mapping error review</td>
</tr>
<tr sizcache="0" sizset="26">
<td valign="top" width="263" sizcache="0" sizset="26"><a class="lightbox" title="Step 8" href="/resources/p447/8.jpg" jquery1267071925472="26" jquery1267072108070="26"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 8" border="0" alt="Step 8" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/8.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Execution step</td>
</tr>
<tr sizcache="0" sizset="27">
<td valign="top" width="263" sizcache="0" sizset="27"><a class="lightbox" title="Step 9" href="/resources/p447/9.jpg" jquery1267071925472="28" jquery1267072108070="28"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 9" border="0" alt="Step 9" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/9.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">Execution results</td>
</tr>
<tr sizcache="0" sizset="28">
<td valign="top" width="263" sizcache="0" sizset="28"><a class="lightbox" title="Step 10" href="/resources/p447/10.jpg" jquery1267071925472="30" jquery1267072108070="30"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Step 10" border="0" alt="Step 10" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/02/10.jpg" width="244" height="178" /></a> </td>
<td valign="top" width="296">
<p>Script errors will appear on the last screen.&#160; Make the necessary changes, click Apply Changes, and Recreate Objects for each error.&#160; When finished, click next to finish.</p>
<p>This is where you’ll see varchar(-1) for conversion from varchar(max) fields.&#160; Change this to text or varchar(n).</p>
<p>Note that sql bit fields convert to smallint(4) if you wish to change that back to bit.</p>
</td>
</tr>
</tbody>
</table>
<p>&#160;</p>
<h3 sizcache="0" sizset="29"><a name="l2s">Linq to Sql Classes to Dblinq</a></h3>
<p>After fixing errors and executing the script, you should now have your full schema migrated to MySql. Now you need to convert your Linq to Sql to Dblinq. Mono uses Dblinq as its Linq to Sql replacement internally. I chose to manually use DBlinq because I wasn’t sure how much of the Dblinq code was available in Mono.&#160; I downloaded the current version of the Dblinq project from their public Subversion repository.</p>
<p>So the basic idea here is that we’re going to swap out L2S with Dblinq in a nearly transparent way.&#160; We’ll drop the existing DBML file from our project, use DBMetal.exe to regenerate new classes, and then update our DBFactory utility function to create a new data context from the DBlinq side.&#160;&#160; Essentially, this will allow us to re-use the same operational syntax in the Repository class with minimal changes.</p>
<p>For generating new classes, I used the DBMetal utility that comes with Dblinq. Here&#8217;s the batch file I setup to generate my code: </p>
<pre class="brush: bash; gutter: false;">REM: note that the '-sprocs' option is turned on

&quot;DbMetal.exe&quot; --provider=MySql -database:sitemanager_mono -server:carbuncle -user:user -password:pass &quot;-namespace:IWS.SiteManager.Core.Model&quot; -code:Generated.cs -sprocs --pluralize</pre>
<p>After generating the classes, I replaced the DBFactory GetProvider function with this: </p>
<pre class="brush: csharp; gutter: false;">public static Model.SiteManager GetProvider()
{
    var con_string = GetDefaultConnectionString();
    return new Model.SiteManager(new MySql.Data.MySqlClient.MySqlConnection(con_string));
}</pre>
<p>I used a repository class as a helper to access my data model. Each repository class contains its own instance of the data context (in this case, Data.SiteManager). During construction of the repository class, it creates a new instance of the data context so any of the factory functions are able to use it without maintaining their own context. So I replaced all the member variable types for existing data contexts with the new one generated from Dblinq. I also noticed Dblinq had different casing than the original column names.&#160; So where I had columns like, “ModifiedUTC” before, it was “ModifiedUtc” now. This was a pretty painless fix with find/replace.</p>
<p>You’ll notice here that the Linq syntax itself didn’t change at all.</p>
<pre class="brush: csharp; gutter: false;">public class ContentRepository
{
    SiteManager _DB;

    public ContentRepository()
    {
        _DB = DBFactory.GetProvider();
    }

    /// &lt;summary&gt;
    /// gets a page by its id
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;id&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetContent(int id)
    {
        return _DB.Contents.Where(o =&gt; o.ID == id).SingleOrDefault();
    }
    /// &lt;summary&gt;
    /// Load a page by its permalink.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;permalink&quot;&gt;&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetPage(string section, string permalink)
    {
        return _DB.Contents.Where(o =&gt; o.SectionID == section &amp;&amp; o.Permalink == permalink.Trim().ToLower()).SingleOrDefault();
    }
    /// &lt;summary&gt;
    /// gets the configured default page.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    public Content GetIndexPage(string section)
    {
        var data = _DB.Sections.Where(o =&gt; o.ID == section).SingleOrDefault();

        Content page = null;

        if (data != null &amp;&amp; data.DefaultContentID.HasValue)
            page = _DB.Contents.Where(o =&gt; o.ID == data.DefaultContentID.Value).SingleOrDefault;

        return page;
    }
    public string GetSectionIDForPage(int id)
    {
        var section = _DB.Contents.Where(o =&gt; o.ID == id).Select(o =&gt; o.SectionID).SingleOrDefault();
        return section;
    }
    public void SaveContent(Content content)
    {
        Section section = null;

        if (_DB.Sections.Where(o =&gt; o.ID == content.SectionID).Count() == 0)
        {
            section = new Section();
            section.ID = content.SectionID;
            section.Description = &quot;Auto-generated section for new page.&quot;;
            _DB.Sections.InsertOnSubmit(section);
            _DB.SubmitChanges();
        }

        //take current version, push it to content history, overwrite current with new.
        Content old = this.GetContent(content.ID);
        if (old != null)
        {
            //archive old version.
            ContentHistory row = new ContentHistory();
            row.ContentID = content.ID;
            row.Body = old.Body;
            row.CreatedUtc = DateTime.UtcNow;
            _DB.ContentHistories.InsertOnSubmit(row);
            _DB.SubmitChanges();

            old.LoadFromExisting(content); //load current values
        }
        else
        {
            old = content;
            _DB.Contents.InsertOnSubmit(old);
        }

        _DB.SubmitChanges();

        if (section != null) //was a new one. set the default content id.
        {
            section.DefaultContentID = content.ID;
            _DB.SubmitChanges();
        }
    }
    public void DeleteContent(int id)
    {
        var data = _DB.Contents.Where(o =&gt; o.ID == id).SingleOrDefault();
        if (data != null)
        {
            //remove conflicts first.
            var section = this.GetSectionByID(data.SectionID);
            if (section != null)
            {
                section.DefaultContentID = null;
                _DB.SubmitChanges();
            }

            _DB.Contents.DeleteOnSubmit(data);
            _DB.SubmitChanges();
        }
    }
}</pre>
<p>At this point, the project should be compilable. You may need to make a few tweaks to get the loose ends tied up.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="30"><a name="membership">Membership Provider</a></h3>
<p sizcache="0" sizset="31">After following the <a href="http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/" target="_blank">instructions from the last post</a>, you should get your membership provider setup and configured. For Mono, I think the biggest hang up for me was not being able to use hashed passwords. Alternatively, you can investigate using encrypted passwords. For simplicity, I used clear text passwords here.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="32"><a name="authorization">Membership Authorization</a></h3>
<p>You should now have a runable MVC application. Almost everything should work with exception to how security applies rules from the web.config.&#160; I noticed Mono doesn’t apply the same security authorization rules to paths for MVC like it does for web forms. I removed &lt;location path=””&gt; tags from my config file and placed the authorization restrictions to my controller actions instead. </p>
<p>So for example this config section would be used to restrict access to all actions in the Manage controller on Windows.</p>
<pre class="brush: xml; gutter: false;">&lt;location path=&quot;Manage&quot;&gt;
    &lt;system.web&gt;
        &lt;authorization&gt;
            &lt;allow roles=&quot;Admins,Editors&quot;/&gt;
            &lt;deny users=&quot;*&quot;/&gt;
        &lt;/authorization&gt;
    &lt;/system.web&gt;
&lt;/location&gt;</pre>
<p>So on Mono, we remove it from web.config and move to the manage controller as an attribute on ALL actions you want to restrict. This can become a maintenance hassle, but they may eventually fix this for MVC in Mono.</p>
<pre class="brush: csharp; gutter: false;">[Authorize(Roles=&quot;Admins,Editors&quot;)]
public ActionResult Index()
{
    return View();
}</pre>
<h3>&#160;</h3>
<h3 sizcache="0" sizset="33"><a name="deployment">Deployment</a></h3>
<p>Don’t forget to make changes to your web.config file. Include the credentials you setup for your database earlier.&#160; Then using Build-&gt;Publish, publish the site to a local folder. Don’t include the App_Data folder and have it delete all files before publishing. Then using FileZilla over SFTP, connect to your server and upload the published website to the web folder you configured. With the latest Mono, the website should automatically reload the new assemblies. If you have any doubt about this, you can always SSH into the server and restart apache.</p>
<p>To restart apache, gain root access using <strong>su</strong> and then: </p>
<pre class="brush: bash; gutter: false;">service apache2 restart</pre>
<p>Optionally, if restarting apache doesn’t seem to work, you can also search and kill off the mono processes:</p>
<pre class="brush: bash; gutter: false;">diabolos:/home/nathan # ps -A | grep mono
 2162 ?        00:00:03 mono
 2164 ?        00:00:08 mono
diabolos:/home/nathan # kill 2162
diabolos:/home/nathan # kill 2164</pre>
<p>The next request to the server will restart these processes as needed. </p>
<p>&#160;</p>
<h3 sizcache="0" sizset="34"><a name="vim">Quick Guide to VIM</a></h3>
<p>Every once in awhile, you may need to edit files that live on the server without going through the whole process of publishing, deploying, etc. To do this, I like to use a tool called VIM. So what is VIM?&#160; For those of you haven’t used it, it’s a simple text editor in Linux that comes with most distributions. You can think of VIM like “edit” for DOS on steroids. The OpenSuse version even has code completion and color-coded syntax, which is very neat.</p>
<p>To open a file with VIM, SSH into your server and run this command:</p>
<p>vi <em>filename</em></p>
<p>When you open the file, there isn’t a menu, but you’ll see ~ on the left and the text of your file.&#160; VIM has modes (much more than I’ll discuss here), but you need to know of at least two:&#160; insert mode and append mode.&#160; This will allow you to insert or append text starting at the cursor position. So move your cursor to any location and press <em><strong>i</strong></em> for insert mode. You will be able to freely type almost anything. To exit insert mode, hit <em><strong>Esc</strong></em>. Append mode uses the same behavior by pressing <em><strong>a</strong></em>. You will append text starting at the cursor position. Entering <strong><em>:d</em></strong> will delete the current line. Be sure not to use arrow keys while in an editing mode.</p>
<p>To save a file, press <strong><em>Esc </em></strong>to exit editing mode, and then press<strong><em> :w</em></strong> to write the file. You can then enter <strong><em>:q</em></strong> to quit.&#160;&#160; And to save and quit, use <strong><em>:wq.</em></strong>&#160; You can get more info by entering <strong><em>man vi</em></strong> at the command prompt.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="35"><a name="wrapup">Wrap Up</a></h3>
<p>So that’s it. This is a tiny project, but you may be able to apply these ideas to your own projects.&#160; For me, this gets my imagination fired up about what kinds of solutions I can take advantage of using Mono. I think there’s a lot of potential here.&#160; I’ve recently begun using interfaces and dependency injection with my projects for data frameworks to avoid the mess we covered in the Linq to Sql to Dblinq section. This means I can write a decoupled application that can optionally use Linq to Sql or Dblinq as a pluggable assembly that is injected using settings in my config file. This will make the transition between Windows and Mono much less painful.</p>
<p>&#160;</p>
<h3 sizcache="0" sizset="36"><a name="source">Get the Source</a></h3>
<p sizcache="0" sizset="37">To obtain the source, head over to <a href="http://sitemanager.codeplex.com/SourceControl/list/changesets" target="_blank">sitemanager.codeplex.com</a> and download the latest source.&#160; You’ll find the mono ported code under the /branches/mono-mysql-fork branch. </p>
<p>&#160;</p>
<h3>Useful Links</h3>
<ul sizcache="0" sizset="38">
<li sizcache="0" sizset="38"><a href="http://www.mono-project.com" target="_blank">Mono project</a> </li>
<li sizcache="0" sizset="39"><a href="http://mono-project.com/Start" target="_blank">Getting Started with Mono</a> </li>
<li sizcache="0" sizset="40"><a href="http://mono-project.com/Guide:_Porting_ASP.NET_Applications" target="_blank">Porting Asp.Net Applications</a> </li>
<li sizcache="0" sizset="41"><a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql .NET Connector</a> (I downloaded mono build &amp; source) </li>
<li sizcache="0" sizset="42"><a href="https://sitemanager.svn.codeplex.com/svn/branches/mono-mysql-fork" target="_blank">SiteManager CodePlex Mono Branch in Subversion</a> </li>
<li sizcache="0" sizset="43"><a href="http://linq.to/db" target="_blank">Dblinq website</a> </li>
<li sizcache="0" sizset="44"><a href="http://dblinq2007.googlecode.com/svn/trunk/" target="_blank">Dblinq Subversion</a> </li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2010/02/walkthrough-porting-asp-net-mvc-website-to-mono-2-6-1-and-mysql-on-linux-apache-porting-to-mono-part-3-of-3/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to Setup and Configure MySql Membership Provider 6.2.2 &#8211; Porting to Mono Part 2 of 3</title>
		<link>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/</link>
		<comments>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 06:00:00 +0000</pubDate>
		<dc:creator>Nathan</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Configuration Tool]]></category>
		<category><![CDATA[Membership]]></category>
		<category><![CDATA[Mono]]></category>
		<category><![CDATA[MySql]]></category>
		<category><![CDATA[MySql.Web]]></category>

		<guid isPermaLink="false">http://www.integratedwebsystems.com/?p=448</guid>
		<description><![CDATA[As the second part of this series, I’ll look at building a...]]></description>
			<content:encoded><![CDATA[<p>As the second part of this series, I’ll look at building a website that can use the MySql Membership Provider. This is one of the main hang-ups I’ve run into while porting one of my ASP.NET applications to Mono. With the latest MySql Connector, this turns out to be a very easy thing to do. It’s nearly as simple as setting up your web.config with the correct parameters and getting the right connector version.</p>
<p> <span id="more-448"></span><br />
<h3>Download the MySql Connector</h3>
<p>Fortunately now, the MySql.Web assembly comes with the prebuilt versions of the <a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql Connector/Net</a>. If you don’t have it, <a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">go get it</a>.&#160; I recommend the Mono build, even for the Windows folks, because it’s a simple zip file with the assemblies. If you would like to see additional samples, download the Source version of their connector. When you have it downloaded, <strong>add a reference to MySql.Web</strong> in your web project.</p>
<h3>&#160;</h3>
<h3>Database</h3>
<p>As with the Microsoft Sql Server membership provider, you have a choice to place your database tables in an existing application database or in its own database. I prefer to use its own just to keep it separated in case I want to share the Membership component among several applications. If you build upon my <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/" target="_blank">last post</a>, you can simply create an empty database for your membership data.&#160; Don’t create any tables yet. Just create an empty shell and create a user account for the application to connect your database and has access to create and alter tables. If you’re new to MySql, the <a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql Administration Tools</a> are extremely handy for this.</p>
<p>&#160;</p>
<h3>Configuration</h3>
<p>The configuration options are mostly the same as the ones you’ve seen in the Microsoft Sql Server membership provider. This one has one added feature, which is an autogenerateschema flag.&#160; This flag instructs the membership provider to actually push schema updates based on the version you’re running. To enable it, simply set it to true.&#160; As with other membership configuration, once you have it enabled and configured to connect to your database, it should work immediately.</p>
<p>There are a few caveats that I ran into while configuring this. For example, hashed passwords only works on Windows .Net; not Mono. Encrypted passwords takes a little more configuration and also (through practice here) only seemed to work on Windows .Net.&#160; There may be some bugs in my version of Mono 2.6.1 or maybe another configuration oddity I couldn’t identify.</p>
<p>Nonetheless, the MySql providers work 100% on Windows. If you’re running on Mono, your main option is to use Clear Text passwords; or if you want to get a little creative, download the source code to the MySql Connector and rewire how it runs its encryption through the framework and roll your own instead.</p>
<p>&#160;</p>
<h3>Configuration Tool</h3>
<p>So moving forward, I built a tiny little configuration tool that you can use to generate the web.config sections you need for this. It takes into account some of my experiences with the things I mentioned like Hashed and Encrypted passwords. It will also auto-generate your MachineKey section if you choose to use encrypted passwords on the Windows side.</p>
<p><a class="launch_tool" href="#" jquery1265988263049="49" jquery1265988344594="2">Launch Configuration Tool</a></p>
<p>Here is the default, Windows based configuration that is generated. You may use this and tweak it or generate your own. </p>
<pre class="brush: xml; gutter: false;">
&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;configuration&gt;
    &lt;connectionStrings&gt;
        &lt;add name=&quot;MySqlMembershipConnection&quot;
            connectionString=&quot;Data Source=server_name;user id=username;password=password;database=database_name;&quot;
            providerName=&quot;MySql.Data.MySqlClient&quot;/&gt;
    &lt;/connectionStrings&gt;

    &lt;system.web&gt;

        &lt;authentication mode=&quot;Forms&quot;&gt;
            &lt;forms
              loginUrl=&quot;~/Account/Logon&quot;

              timeout=&quot;30&quot;
              name=&quot;.ASPXFORM$&quot;
              path=&quot;/&quot;
              requireSSL=&quot;false&quot;
              slidingExpiration=&quot;true&quot;
              defaultUrl=&quot;Default.aspx&quot;

              enableCrossAppRedirects=&quot;false&quot;/&gt;
        &lt;/authentication&gt;

        &lt;membership defaultProvider=&quot;MySqlMembershipProvider&quot;&gt;
            &lt;providers&gt;
                &lt;clear/&gt;
                &lt;add name=&quot;MySqlMembershipProvider&quot;
                      type=&quot;MySql.Web.Security.MySQLMembershipProvider, mysql.web&quot;
                        connectionStringName=&quot;MySqlMembershipConnection&quot;
                        enablePasswordRetrieval=&quot;false&quot;
                        enablePasswordReset=&quot;true&quot;
                        requiresQuestionAndAnswer=&quot;false&quot;
                        requiresUniqueEmail=&quot;true&quot;
                        passwordFormat=&quot;Hashed&quot;
                        maxInvalidPasswordAttempts=&quot;5&quot;
                        minRequiredPasswordLength=&quot;6&quot;
                        minRequiredNonalphanumericCharacters=&quot;0&quot;
                        passwordAttemptWindow=&quot;10&quot;

                        applicationName=&quot;/&quot;
                        autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/membership&gt;

        &lt;roleManager enabled=&quot;true&quot; defaultProvider=&quot;MySqlRoleProvider&quot;&gt;
            &lt;providers&gt;
                &lt;clear /&gt;
                &lt;add connectionStringName=&quot;MySqlMembershipConnection&quot;
                    applicationName=&quot;/&quot;
                    name=&quot;MySqlRoleProvider&quot;
                    type=&quot;MySql.Web.Security.MySQLRoleProvider, mysql.web&quot;
                    autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/roleManager&gt;

        &lt;profile&gt;
            &lt;providers&gt;
                &lt;clear/&gt;
                &lt;add type=&quot;MySql.Web.Security.MySqlProfileProvider, mysql.web&quot;
                      name=&quot;MySqlProfileProvider&quot;
                      applicationName=&quot;/&quot;
                      connectionStringName=&quot;MySqlMembershipConnection&quot;
                      autogenerateschema=&quot;true&quot;/&gt;
            &lt;/providers&gt;
        &lt;/profile&gt;
    &lt;/system.web&gt;
&lt;/configuration&gt;
</pre>
<h3>Testing Membership</h3>
<p>So once you have your membership configured, you’ll need to test it out. First, don&#8217;t forget to update your connection string credentials. Then the easiest way to test your membership configuration (if you are using Visual Studio) is to use the ASP.NET Configuration Tool (appears when selecting your web project). <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="Solution Toolbar" border="0" alt="Solution Toolbar" align="right" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/Toolbar.jpg" width="368" height="86" />When membership configuration is working properly the landing page will show you how many users are configured in the system. If there are none, it will still show you zero. You will also have access to add, remove, and update users from this screen.&#160; If it doesn’t show you anything, then there’s likely something wrong in your configuration.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="aspnetConfig" border="0" alt="aspnetConfig" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/aspnetConfig.jpg" width="512" height="180" /></p>
<p>&#160;</p>
<p>After the first time testing your membership, you’ll notice all the schema in the database will have been generated and is now filled with data. This was caused by the<em> autogeneratechema</em> flag being set to “true”.&#160; As new versions of MySql.Web assembly are released from MySql, any schema changes new to each version will automatically be applied with this flag enabled. It should also alleviate that “Schema is missing or incorrect” error.</p>
<p><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="database" border="0" alt="database" src="http://www.integratedwebsystems.com/wp-content/uploads/2010/01/database.jpg" width="602" height="266" /></p>
<p>&#160;</p>
<h3>Wrap Up</h3>
<p>So with that, you should have your ASP.NET application configured to run membership to a MySql database. If you’re using Mono, this configuration should work with the default ASP.NET MVC web application (after you reference the MySql assemblies and setup the configuration of course).&#160; Look forward to my next post, which will be the final part of this series about porting an ASP.NET MVC application to Mono. We’ll be taking a very simple MVC application called <a href="http://sitemanager.codeplex.com" target="_blank">SiteManager on CodePlex</a> and convert it to run on Mono and MySql.</p>
<p>&#160;</p>
<h3>Useful Links</h3>
<ul>
<li><a href="http://dev.mysql.com/downloads/mysql/" target="_blank">MySql Community Server</a>&#160; &#8211; Installing on OpenSuse Linux? <a href="http://www.integratedwebsystems.com/2010/01/installing-opensuse-11-2-with-mono-2-6-1-and-apache-using-text-mode-configuration-porting-to-mono-part-1-of-3/#InstallMonoAndApache">Refer to my last post</a>. </li>
<li><a href="http://dev.mysql.com/downloads/workbench/5.2.html" target="_blank">MySql GUI Tools</a> </li>
<li><a href="http://dev.mysql.com/downloads/connector/net/" target="_blank">MySql Connector/NET</a> </li>
<li><a class="launch_tool" href="#">My Cool Configuration Tool</a> </li>
</ul>
<p><div id="dialogContent"></div>
<link href="/tools/css/smoothness/jquery-ui-1.7.2.custom.css" rel="stylesheet" type="text/css" />
<link href="/tools/css/p448.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="http://www.google.com/jsapi"></script><br />
	<script type="text/javascript">
		google.load("jqueryui", "1.7.2");
	</script><br />
	<script language="javascript" type="text/javascript" src="/tools/js/p448.js"></script><br />
	<script src="/tools/js/MicrosoftAjax.js" type="text/javascript"></script><br />
	<script src="/tools/js/MicrosoftMvcAjax.js" type="text/javascript"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

