Background
This started out as an experiment with ASP.NET MVC. When you create a MVC project it auto-creates the membership functionality for you. By default the membership provider it uses is the local MSSQL Express instance. This is great but I host my sites with a shared provider and MSSQL Express is not available. I could use MSSQL but am limited to the number of databases I can create, or I can use MySql. So I started looking for a MySql membership provider solution. I found an article by Nolan Bailey and he covers everything but I wanted to be a little more explicit about the steps.
Required Software
Before you start you need to download:
MySql – I recommend the Community Server Edition (Why? Because it is Free.)
MySql Connector/Net — for connecting to MySql from .NET (Get the latest version)
MySql Admin – GUI to manage your MySql instance (Not necessary but it helps to visually verify everything is working properly)
Process Steps
- Install MySql ( I downloaded the .MSI installer. I like this because it registers all the services, .DLL’s, etc.)
- Launch the MSI
- Follow the onscreen instructions
- Remember the password you enter, you will need it.
- Install MySql Connector/Net (Again I like the .MSI because it registers everything for me)
- Launch the MSI
- Follow the onscreen instructions
- I choose the complete install
- Install MySql Admin
- Launch the MSI
- Follow the onscreen instructions
- Create a New Project in VS 2008
- Click File – New Project
- Set the Name and Location Information
- Click OK
- Create the reference to the required DLL files
- In “Solution Explorer” expand your project and find “References”
- Right Click “References” and click “Add Reference…”
- Click on the “Browse” tab
- Browse to “C:\Program Files\MySQL\MySQL Connector Net 6.0.4\Assemblies” (This will be different depending on the version of MySql Connector/Net you downloaded)
- Select “MySql.Data.dll”
- Click OK
- Expand “References”
- Right-Click on MySql.Data and choose properties
- Change Copy Local to true
- Right Click “References” and click “Add Reference…”
- Click on the “Browse” tab
- Browse to “C:\Program Files\MySQL\MySQL Connector Net 6.0.4\Assemblies” (This will be different depending on the version of MySql Connector/Net you downloaded)
- Select “MySql.Web.dll”
- Click OK
- Expand “References”
- Right-Click on MySql.Web and choose properties
- Change Copy Local to true
- Modify the Web.config
- Create your connection string
- It should look something like: <add name="MySqlApplicationServices" connectionString="Server=localhost;Database=<Membership Security Schema>;User ID=root;Password=<from step 1.3>;" providerName="MySql.Data.MySqlClient" />
- Replace <Membership Security Schema> with the name of the database you want to use for your project
- Replace <from step 1.3> with your actual password
- Copy settings from machine.config to web.config
- Click File – Open File…
- Browse to x:\Windows\Microsoft.NET\Framework\<version>\config\machine.config (<version> will depend on version of .NET you are targeting. In the case of 2 and 3.5 it is v2.0.50727)
- Select machine.config and click “Open”
- Locate the section <system.web>
- Copy the following sections from your machine.config to your web.config <system.web> section
- <Membership>
- <roleManager>
- <profile>
- Update the web.config settings
- You will need to update the connectionStringName for each provider to correspond to the connection string names in your web.config
- Example: <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.0.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="MySqlApplicationServices" applicationName="/" />
- Add autogenerateschema=”true” to the membership provider string for your MySql instance
- Example: <add connectionStringName="MySqlApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.0.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" autogenerateschema="true" />
- Change your provider settings
- On the “Project” Menu select “ASP.NET Configuration”
- It will launch a web browser
- Click on the “Provider” tab
- Choose the link “Select a different provider for each feature (advanced)”
- Select the MySql providers they will be listed as the name=”” attribute from their configuration strings in the web.config
- Close the web browser
- Setup the Database (Schema) – If you don’t setup the database you will get the error “Invalid schema or catalog specified for provider” or something similar when you run your application.
You have now setup MySql as your Membership provider. The first time your application attempts to use the membership functionality the proper tables will be created automatically.
Migrating the database to your production environment is well documented so I won’t go into that here. Just remember to update your connection string.
I hope this helps.
Currently rated 4.2 by 9 people
- Currently 4.222222/5 Stars.
- 1
- 2
- 3
- 4
- 5