January 2010 - One post
Posted on the 10th at 7:07 PM CST
ASP.NET Membership, Profile, and Role Providers for MySQL
FiledFiled under ASP.NET, MySQL

I am a huge fan of MySQL, primarily because it is free and fast. While it does not have the endless ensemble of features that SQL Server does, MySQL can certainly hold its own. And it works perfect for personal projects because it is portable, not to mention that purchasing SQL Server practically requires mortgage. I use the MySQL GUI Tools for basic queries and the excellent .NET Connector for programmatibility, both of which are graciously provide for free. Can't beat it! However, I will not lie and say that everything is as seamless as it is with SQL Server. Projects like SubSonic are targeted for SQL Server, and while they claim to "support" MySQL, it seldom works as cleanly as the SQL Server demo videos. In this post, I'll walk you through setting up the Membership, Profile, and Role providers for ASP.NET using MySQL as the backend. I struggled with setting this up initially, and did not have any luck finding resources on the web, so I am doing my duty. My environment is this:

  • Visual Studio 2008, .NET 3.5 SP1
  • MySQL Server version 5.1, Connector/Net version 6.2.2
  • Windows XP 64-bit

Once you have the .NET connector installed, you'll need to add a reference to MySql.Web.dll to your project. Then you are able to tweak the web.config file accordingly and add your provider references. They exist in the MySql.Web.Security namespace, with the exception of Profile (it has its own namespace). This is where I ran into problems because although I knew the name of the assembly, I did not know the rest of the required details. The default provider for ASP.NET in web.config looks like this…

<add name="AspNetSqlMembershipProvider"
     type="System.Web.Security.SqlMembershipProvider,
           System.Web,
           Version=2.0.0.0,
           Culture=neutral,
           PublicKeyToken=b03f5f7f11d50a3a"
     connectionStringName="ApplicationServices"
     enablePasswordRetrieval="false"
     enablePasswordReset="true"
     requiresQuestionAndAnswer="false"
     requiresUniqueEmail="false"
     passwordFormat="Hashed"
     maxInvalidPasswordAttempts="5"
     minRequiredPasswordLength="6"
     minRequiredNonalphanumericCharacters="0"
     passwordAttemptWindow="10"
     passwordStrengthRegularExpression=""
     applicationName="NAME_OF_YOUR_PROJECT"
  />


The type attribute is the most daunting. I can guess that the first part is the full reference to the assembly and the second one is the name of the DLL (the namespace). I can find the version of the MySQL DLL in the "Add Reference" dialog. I guess I can just try Culture=neutral, that sounds about right. But what about PublicKeyToken? What the hell is this? I tried a bunch of things, but ultimately it was never able to connect to MySQL successfully without a correct token value.

Getting the PublicKeyToken for a .NET Assembly (DLL)

Using the Visual Studio 2008 command prompt, type in the following command:

sn -T "C:\Path\To\Assembly.dll"


And this will give you the PublicKeyToken value. Thank you, Google!

With that bit of information out of the way, I was ready to try getting the Security setup. In the affected sections of web.config, the important attributes are "connectionStringName" and "autogenerateschema". The connectionStringName should point to your connection string within the web.config file. The autogenerateschema will automatically generate your tables in MySQL (sweet!), but you only need this on the membership element. When it was all said and done, that section of my web.config file looked like this…

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


Now go ahead and launch the ASP.NET Configuration tool (from the Project menu). If you can click on the Security tab without seeing an error, you are golden! I haven't experienced any problems thus far, but if I do, I will be sure to update this post and/or add a comment.

Thanks for stopping by! Look for more posts to come in the near future.

Comments (7)
Permalink Comment from chinmi on January 16th, 2010 at 4:29 PM
great writeup.. thanks a lot!!
Permalink Comment from Peter Kellner on February 6th, 2010 at 11:49 PM
Where did you find MySql.Web.Security.MySQLMembershipProvider? Years ago, I found one on the web and modified it a bunch (and later found it had lots of bugs, I found them one at a time). How is the latest connector for mysql? A few years ago, that was pretty buggy also. I did one mysql project and it worked great until it got busy, than it fell apart.
Permalink Comment from Josh StodolaEmail on February 8th, 2010 at 5:06 PM
@Peter I was browsing through the documentation and noticed the membership provider. Haven't used it in a large app yet, so not sure on the reliability of it, but it seems to do its job fine. The .NET connector has been fantastic, in my experience. I don't have any problems with it. It has been powering this blog since the beginning, and I've used MySQL on several other projects using the connector without issues. I do remember when it used to be buggy way back when it was fairly fresh, but they've ironed out plenty of issues since then. I should mention that my databases are not ridiculously complex (I don't have any sprocs, for instance).
Permalink Comment from Peter Kellner on February 8th, 2010 at 11:07 PM
Thanks for the update on mysql. I'll make sure I get a fresh copy if I go at it again.
Permalink Comment from 1212 on February 25th, 2010 at 4:01 AM
detected by spam bots
Comments are limited to 3,000 characters; so far you have used none of them
HTML will be encoded; links and line breaks will be converted automatically
Permalink Comment from seyi on March 8th, 2010 at 7:58 AM
before i read your article, i had no problem setting up my MYSQL membership until it was time to publish on the internet.

Am using a Mysql 6.2.2.0 connector as refrence to my Aspnet 2.0 application.


i have the following errors:Could not load file or assembly 'MySQL.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d' or one of its dependencies. The system cannot find the file specified.
please kindly help me look into it.

please note that it works fine on my local system but throws that error on the server.
Permalink Comment from Josh StodolaEmail on March 10th, 2010 at 8:42 AM
@seyi Make sure to register the DLL on the server using regsvr32

Guess What?

There are a few basic guidelines you should be aware of before leaving a comment…

  • If you choose to display your email address, it will not be detected by spam bots
  • Comments are limited to 3,000 characters; so far you have used none of them
  • HTML will be encoded; links and line breaks will be converted automatically
  • Comments containing five or more links will be subject to moderation

Have Your Say

← Answer this to prove you are human
 
 

Chill Out…