Spec-Zone .ru
спецификации, руководства, описания, API

22.2.5.18. ASP.NET Provider Model

MySQL Connector/Net provides support for the ASP.NET 2.0 provider model. 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.

MySQL Connector/Net supplies the following providers:

The following tables show the supported providers, their default provider and the corresponding MySQL provider.

Membership Provider

Default Provider MySQL Provider
System.Web.Security.SqlMembershipProvider MySql.Web.Security.MySQLMembershipProvider

Role Provider

Default Provider MySQL Provider
System.Web.Security.SqlRoleProvider MySql.Web.Security.MySQLRoleProvider

Profile Provider

Default Provider MySQL Provider
System.Web.Profile.SqlProfileProvider MySql.Web.Profile.MySQLProfileProvider

SessionState Provider

Default Provider MySQL Provider
System.Web.SessionState.InProcSessionStateStore MySql.Web.SessionState.MySqlSessionStateStore
Note

The MySQL Session State provider uses slightly different capitalization on the class name compared to the other MySQL providers.

Installing The Providers

The installation of Connector/Net 5.1 or later 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 SessionState 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 MySQL 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.

Using The Providers

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>

Note the database to connect to must be specified.

Rather than manually editing configuration files, consider using the MySQL Website Configuration tool to configure your desired provider setup. From MySQL Connector/Net 6.1.1 onwards, all providers can be selected and configured from this wizard. The tool modifies your website.config file to the desired configuration. A tutorial on doing this is available in the following section Section 22.2.3.12, "MySQL Website Configuration Tool".

A tutorial demonstrating how to use the Membership and Role Providers can be found in the following section Section 22.2.4.2, "Tutorial: MySQL 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.