MySQL Connector/NET Developer Guide
MySQL Connector/NET includes a provider model for use with ASP.NET. This model enables application developers to focus on the business logic of their application instead of having to recreate such boilerplate items as membership and roles support.
Connector/NET supplies the following web providers:
Membership provider
Roles provider
Profiles provider
Session state provider
The following tables show the supported providers, their default provider and the corresponding MySQL provider.
Default Provider | MySQL Provider |
---|---|
System.Web.Security.SqlMembershipProvider |
MySql.Web.Security.MySQLMembershipProvider |
Default Provider | MySQL Provider |
---|---|
System.Web.Security.SqlRoleProvider |
MySql.Web.Security.MySQLRoleProvider |
Default Provider | MySQL Provider |
---|---|
System.Web.Profile.SqlProfileProvider |
MySql.Web.Profile.MySQLProfileProvider |
Default Provider | MySQL Provider |
---|---|
System.Web.SessionState.InProcSessionStateStore |
MySql.Web.SessionState.MySqlSessionStateStore |
The MySQL session state provider uses slightly different capitalization on the class name compared to the other MySQL providers.
The installation of Connector/NET will install the providers and register
them in your machine's .NET configuration file,
machine.config
. The additional entries
created will result in the system.web
section
appearing similar to the following code:
<system.web> <processModel autoConfig="true" /> <httpHandlers /> <membership> <providers> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" /> <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.1.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression="" /> </providers> </membership> <profile> <providers> <add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.1.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /> </providers> </profile> <roleManager> <providers> <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.1.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /> </providers> </roleManager> </system.web>
Each provider type can have multiple provider implementations. The
default provider can also be set here using the
defaultProvider
attribute, but usually this is
set in the web.config
file either manually or
by using the ASP.NET configuration tool.
At time of writing, the MySqlSessionStateStore
is not added to machine.config
at install
time, and so add the following:
<sessionState> <providers> <add name="MySqlSessionStateStore" type="MySql.Web.SessionState.MySqlSessionStateStore, MySql.Web, Version=6.1.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" /> </providers> </sessionState>
The session state provider uses the
customProvider
attribute, rather than
defaultProvider
, to set the provider as the
default. A typical web.config
file might
contain:
<system.web> <membership defaultProvider="MySQLMembershipProvider" /> <roleManager defaultProvider="MySQLRoleProvider" /> <profile defaultProvider="MySQLProfileProvider" /> <sessionState customProvider="MySqlSessionStateStore" /> <compilation debug="false"> ...
This sets the MySQL Providers as the defaults to be used in this web application.
The providers are implemented in the file
mysql.web.dll
and this file can be found in
your Connector/NET installation folder. There is no need to run any type
of SQL script to set up the database schema, as the providers
create and maintain the proper schema automatically.
The easiest way to start using the providers is to use the ASP.NET configuration tool that is available on the Solution Explorer toolbar when you have a website project loaded.
In the web pages that open, you can select the MySQL membership and roles providers by picking a custom provider for each area.
When the provider is installed, it creates a dummy connection
string named LocalMySqlServer
. Although this
has to be done so that the provider will work in the ASP.NET
configuration tool, you override this connection string in your
web.config
file. You do this by first
removing the dummy connection string and then adding in the proper
one, as shown in the following example:
<connectionStrings> <remove name="LocalMySqlServer"/> <add name="LocalMySqlServer" connectionString="server=xxx;uid=xxx;pwd=xxx;database=xxx"/> </connectionStrings>
You must specify the database in this connection.
Rather than manually editing configuration files, consider using
the MySQL Application Configuration tool in MySQL for Visual Studio to configure
your desired provider setup. The tool modifies your
Web.config
file to the desired configuration.
A tutorial on doing this is available in the following section
MySQL Application Configuration Tool.
A tutorial demonstrating how to use the membership and role providers can be found in the following section Section 6.2, “Tutorial: Connector/NET ASP.NET Membership and Role Provider”.
Deployment
To use the providers on a production server, distribute the
MySql.Data
and the MySql.Web
assemblies, and either register them in the remote systems Global
Assembly Cache or keep them in your application's
bin/
directory.