How to Setup and Configure MySql Membership Provider 6.2.2 – Porting to Mono Part 2 of 3

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.

Download the MySql Connector

Fortunately now, the MySql.Web assembly comes with the prebuilt versions of the MySql Connector/Net. If you don’t have it, go get it.  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, add a reference to MySql.Web in your web project.

 

Database

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 last post, you can simply create an empty database for your membership data.  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 MySql Administration Tools are extremely handy for this.

 

Configuration

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.  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.  As with other membership configuration, once you have it enabled and configured to connect to your database, it should work immediately.

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.  There may be some bugs in my version of Mono 2.6.1 or maybe another configuration oddity I couldn’t identify.

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.

 

Configuration Tool

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.

Launch Configuration Tool

Here is the default, Windows based configuration that is generated. You may use this and tweak it or generate your own.

  
<?xml version="1.0"?>
<configuration>
    <connectionStrings>
        <add name="MySqlMembershipConnection"
            connectionString="Data Source=server_name;user id=username;password=password;database=database_name;"
            providerName="MySql.Data.MySqlClient"/>
    </connectionStrings>

    <system.web>


        <authentication mode="Forms">
            <forms
              loginUrl="~/Account/Logon"

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

              enableCrossAppRedirects="false"/>
        </authentication>

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

                        applicationName="/"
                        autogenerateschema="true"/>
            </providers>
        </membership>

        <roleManager enabled="true" defaultProvider="MySqlRoleProvider">
            <providers>
                <clear />
                <add connectionStringName="MySqlMembershipConnection"
                    applicationName="/"
                    name="MySqlRoleProvider"
                    type="MySql.Web.Security.MySQLRoleProvider, mysql.web"
                    autogenerateschema="true"/>
            </providers>
        </roleManager>

        <profile>
            <providers>
                <clear/>
                <add type="MySql.Web.Security.MySqlProfileProvider, mysql.web"
                      name="MySqlProfileProvider"
                      applicationName="/"
                      connectionStringName="MySqlMembershipConnection"
                      autogenerateschema="true"/>
            </providers>
        </profile>
    </system.web>
</configuration>

Testing Membership

So once you have your membership configured, you’ll need to test it out. First, don'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). Solution ToolbarWhen 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.  If it doesn’t show you anything, then there’s likely something wrong in your configuration.

aspnetConfig

 

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 autogeneratechema flag being set to “true”.  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.

database

 

Wrap Up

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).  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 SiteManager on CodePlex and convert it to run on Mono and MySql.

 

Useful Links

comments powered by Disqus