Background
I have recently been delving into ASP.Net, in a bit more detail than previously. I have, for a little while, been wanting to understand how the integrated log-in security worked and found this great walk though from Microsoft. This walk through uses MS SQL Server and I wanted to try using MySQL as well.The main reason I wanted to try this is that one of my colleagues is shortly starting a project using ASP.Net and MySQL and I thought it would be useful to be able to give some useful advice on how to handle role management in this scenario. I have to admit to some personal curiosity as well :-)
The Process
For my first attempt at this I found this tutorial on the MySQL website. Before following the tutorial I downloaded and installed the latest version of MySQL Connector/Net (version 6.7.4 at the time of writing).
The information in the tutorial pertaining to connection strings appears to be incorrect and I was unable to get a connection to the database. A Google search led me to this site giving examples of valid connection strings. I was able to get a connection using the following:
<add name="LocalMySqlServer" connectionString="Server=<server>;Port=3306;Database=test;Uid=<user name>;Pwd=<password>" providerName="MySql.Data.MySqlClient"/>
I was then able to run the ASP.Net Configuration and get the databases created. However, every time I tried to add a new user the system crashed with an exception message that suggested that the MySql.Web assembly could not be loaded.
I eventually realised that the MySQL Connector/Net assemblies were not in the GAC. I tried starting the Visual Studio Command Line and using gacutil.exe to add the assemblies to the GAC, but although it said that the actions had been performed correctly the assemblies did not show up when looking in the GAC (C:\Windows\assembly). I then tried dragging and dropping the assemblies into the GAC, but this did not work either.
Another tutorial that I came across suggested that the installation should have put the assemblies into the GAC automatically, but this referred to an older version of the connector. This led me to the idea that maybe an older version of the connector would install correctly and save me some grief.
I then uninstalled the connector and downloaded and installed version 6.6.5 (the last available version before 6.7.4). After tweaking the config settings slightly I was finally able to get the Create User function to work in the ASP.Net Configuration Tool so that I could create a user.
Tutorial Modification
When following the MySQL tutorial referenced above, the following things need to be borne in mind:
- When installing the MySQL Connector/Net, make sure that the assemblies have been installed into the GAC. If this is not the case, try installing an older version.
- Section 3 of the tutorial can be ignored. With the assemblies in the GAC they don't need to be explicitly referenced in your project.
- Modifying machine.config is unnecessary. Instead, add the connection strings and membership information to your applications web.config file. I have included templates for the relevant sections below, along with some information on how to obtain information for sections you need to fill in yourself.
Hopefully that should be enough information to get you on your way.
web.config
Add a <connectionStrings> section if necessary and then add the following entries:
<remove name="LocalMySqlServer"/> <add name="LocalMySqlServer" connectionString="[1]" providerName="MySql.Data.MySqlClient"/>
[1] You can use the connection strings site referenced above to help work out your connection string if you are not sure what to fill in here.
Add the following section to the root configuration section:
<membership
defaultProvider="MySQLMembershipProvider"
userIsOnlineTimewindow="15">
<providers>
<clear/>
<add
name="MySQLMembershipProvider"
type="[2]"
connectionStringName="LocalMySqlServer"
applicationName="MyApplication"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="true"
passwordFormat="Hashed"
autogenerateschema="true"/>
</providers>
</membership>
[2] You can get the type from machine.config which is located in the Config folder in the appropriate Microsoft.NET/Framework/v[version number] folder. Open the machine.config file in a text editor and do a search for membership. When you find the appropriate membership/providers section, copy the type string from the MySQLMembershiProvider.
For reference, the type string I am using is:
type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.6.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"

