ASP.NET 4 and MySQL Membership Provider

by 5. August 2010 01:07

Recently I had to setup an ASP.NET MVC 2 project which would utilize the built-in membership of ASP.NET. However, I didn't have access to a MS SQL database, so I had to use a MySQL data provider instead. The following is a quick guide on how to get it setup.

First, you need to download the MySQL Connector/Net from this page. This makes it possible to connect to MySQL databases from .NET applications and gives you access to the ADO.NET interfaces. I choose to download the latest development release (at the time of writing) Connector/Net 6.3.3 beta, as this fully integrates with Visual Studio 2010 which the latest public release (6.2.3 at the time of writing) does not. Download BOTH the source ( and the installation file ( I will explain why in a second.

Once you've downloaded both files, extract them and install the connector. Now, normally when using the membership provider, the database tables/schemas are automatically created. The MySQL membership provider does this as well, unfortunately it just doesn't do it right. At least it didn't work for me. Instead, you have to create the databases semi-manually. Go to the location where you extracted the source and browse to the following folder "\MySql.Web\Providers\Properties". In this folder you will see a number of .sql files: schema1.sql, schema2.sql, schema3.sql, schema4.sql, schema5.sql and schema6.sql. Run each of these, in turn and starting with schema1.sql, against your MySQL database.

Now, fire up Visual Studio 2010 and open your application. Add a reference to MySql.Web.dll which can be found in the directory you installed the Connector, e.g. C:\Program Files\MySQL\MySQL Connector Net 6.3.3\Assemblies\v2.0

Next, unless you haven’t done this already, add your MySQL connection string to the configuration/connectionStrings element in the Web.config, e.g.:

<add name="MySQLConn" connectionString="Server=SERVERADDRESS;Database=DATABASENAME;Uid=USERNAME;Pwd=PASSWORD;"/>

Finally, open up your Web.config and add these lines to the <system.web> element:

<membership defaultProvider="MySqlMembershipProvider">
<add name="MySqlMembershipProvider" 
type="MySql.Web.Security.MySQLMembershipProvider,MySql.Web,Version=, Culture=neutral,PublicKeyToken=c5687fc88969c44d" 
autogenerateschema="true" connectionStringName="MySQLConn" 
enablePasswordRetrieval="false" enablePasswordReset="true" 
requiresQuestionAndAnswer="false" requiresUniqueEmail="false" 
passwordFormat="Hashed" maxInvalidPasswordAttempts="5" 
minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" 
passwordAttemptWindow="10" passwordStrengthRegularExpression="" 
applicationName="/" />
<profile defaultProvider="MySqlProfileProvider">
<add name="MySqlProfileProvider" 
connectionStringName="MySQLConn" applicationName="/" />
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<clear />
<add name="MySqlRoleProvider" 
connectionStringName="MySQLConn" applicationName="/" />

Now, you've (hopefully) got a fully working MySQL membership provider. To test it, go to Project > ASP.NET Configuration and go to the Security tab. Here you should be able to manage users and roles.

NOTE: Make sure you enter the correct connectionstring name, version number and PublicKeyToken in the Web.config. The version number is self-explanatory, but the PublicKeyToken can be a little trickier to figure out. The PublicKeyToken value is most likely the same, but to make sure see this link on how to find the PublicKeyToken for a .dll

Tags: , ,