Using MySQL with ASP.NET with Membership and Roles

One of my instructors in college asked me if it’s possible to use MySQL as a storage engine with ASP.NET and likewise use it with membership. I was a bit surprised since I haven’t touched MySQL for quite a long time and while it is possible, I never thought I would be encountering a situation like this.

I already have a WAMP system set-up in my machine, thanks to the XAMPP package for Windows but I only use Mercury in that package for testing sending out of e-mails in my web applications. Further, I have downloaded the latest MySQL connector for .NET applications and so I went.

Firing up Visual Studio 2008, created a .NET 2.0 website and followed this – it worked until step #15. I am not sure if it’s because the version but by default, the MySQL role provider didn’t appear in the website configuration tool. Since the content might change, I got a page print of the current version (as of this writing).

Here are some tips that I will provide:

1. If you want to use the MySqlMembership Provider as well as overriding the default password complexity of ASP.NET membership provider, use this:


 <membership defaultProvider="MySQLSqlMembershipProvider">
 <providers>
 <add connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false"
 autogenerateschema="true" enablePasswordReset="true" requiresQuestionAndAnswer="true"
 applicationName="/" requiresUniqueEmail="true" passwordFormat="Hashed"
 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="5"
 minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="5"
 passwordStrengthRegularExpression="" name="MySQLSqlMembershipProvider"
 type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.1, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
 </providers>
 </membership>

2. Notice the connection string name. The connection string, that you have to use, once you have set-up your MySQL references (see step #3) is this:


<connectionStrings>
 <remove name="LocalMySqlServer"/>
 <add name="LocalMySqlServer"
 connectionString="Datasource=SERVER;Database=YOURDB;uid=YOURUSER;pwd=YOURPASSWORD;"
 providerName="MySql.Data.MySqlClient"/>
 </connectionStrings>

The role provider don’t appear by default and I don’t know why. But that won’t stop you from using the MySQL DB setup with membership with the Role service. Just add these lines to your web.config file and you’re off to it:


<roleManager enabled="true" defaultProvider="MySQLRoleProvider">
 <providers>
 <add applicationName="/" connectionStringName="LocalMySqlServer"
 autogenerateschema="true" name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider" />
 </providers>
 </roleManager>

I now got my application up and running. Now more tests to do. :)

About these ads

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s