Language

Visual Basic

C#

Show All

MyDirect .NETmysqlnet@devart.com

ASP.NET Provider Model Support

MyDirect .NET can be used in ASP.NET 2.0 provider model. It allows developers to write better structured applications and easily switch data storage in ASP.NET application from other media such as Microsoft SQL Server. For detailed information on basics refer to MSDN whitepaper ASP.NET 2.0 Provider Model: Introduction to the Provider Model.

MyDirect .NET implements Membership provider, Role provider, Session State provider and Profile provider. This article provides information on how to set up ASP.NET application to use MyDirect .NET as one of these providers. It consists of following sections:

Installation and setup

All providers are contained in CoreLab.MySql.Web.dll assembly. You have to add reference to this assembly in your project in order to use ASP.NET provider model. Another condition for the providers to function properly is existence of certain database objects. Before using ASP.NET providers you have to run the script InstallWebTables.sql against database you wish to use. The script can be found in folder \Program Files\Devart\MyDirect.NET\.

During the installation process MyDirect .NET adds new entries in machine.config file. They are used by web sites when the settings are not overridden with local Web.config file. You can choose to keep the settings in machine.config, in which case they will be used for all web sites, or in Web.config files, where you can set up specific parameters. Note that if you wish to redefine an identifier in Web.config file you have to add remove keyword in appropriate section, as shown below:
  <remove name="MySqlServices" />
  <add name="MySqlServices" connectionString="User Id=root;Host=localhost;Database=Test" />

This example demonstrates how to adjust connection string parameters. By default MyDirect .NET creates empty connection string with name MySqlServices.

Membership provider

The fundamental job of a membership provider is to interface with data sources containing data regarding a site's registered users, and to provide methods for creating users, deleting users, verifying login credentials, changing passwords, and so on. The .NET Framework's System.Web.Security namespace includes a class named MembershipUser that defines the basic attributes of a membership user and that a membership provider uses to represent individual users.

To access this functionality use CoreLab.MySql.Web.Providers.MySqlMembershipProvider class. It behaves as described in reference for System.Web.Security.MembershipProvider class. The following example shows the Web.config file for an ASP.NET application configured to use MySqlMembershipProvider.

<configuration>
  <connectionStrings>
    <add name="MySqlServices" connectionString="User Id=root;Host=localhost;Database=Test" />
  </connectionStrings>
  <system.web>
    ...
    <membership defaultProvider="AspNetMySqlMembershipProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add
          name="AspNetMySqlMembershipProvider"
          type="CoreLab.MySql.Web.Providers.MySqlMembershipProvider"
          connectionStringName="MySqlServices"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false"
          passwordFormat="Hashed"
          maxInvalidPasswordAttempts="5"
          passwordAttemptWindow="10" />
      </providers>
    </membership>
  </system.web>
</configuration>

Role provider

The fundamental job of a role provider is to interface with data sources containing role data mapping users to roles, and to provide methods for creating roles, deleting roles, adding users to roles, and so on. Given a user name, the role manager relies on the role provider to determine what role or roles the user belongs to. The role manager also implements administrative methods such as Roles.CreateRole and Roles.AddUserToRole by calling the underlying methods in the provider.

To access this functionality use CoreLab.MySql.Web.Providers.MySqlRoleProvider class. It behaves as described in reference for System.Web.Security.RoleProvider class. The following example shows the Web.config file for an ASP.NET application configured to use MySqlRoleProvider. Note that you can configure the MySqlRoleProvider to use the same database and user information as the MySqlMembershipProvider in order to use a single database for authentication and authorization information.

<configuration>
  <connectionStrings>
    <add name="MySqlServices" connectionString="User Id=root;Host=localhost;Database=Test" />
  </connectionStrings>
  <system.web>
    ...
    <membership defaultProvider="AspNetMySqlMembershipProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add
          name="AspNetMySqlMembershipProvider"
          type="CoreLab.MySql.Web.Providers.MySqlMembershipProvider"
          connectionStringName="MySqlServices"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false"
          passwordFormat="Hashed"
          maxInvalidPasswordAttempts="5"
          passwordAttemptWindow="10" />
      </providers>
    </membership>
    <roleManager defaultProvider="AspNetMySqlRoleProvider"
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieProtection="All" >
      <providers>
        <add
          name="AspNetMySqlRoleProvider"
          type="CoreLab.MySql.Web.Providers.MySqlRoleProvider"
          connectionStringName="MySqlServices" />
      </providers>
    </roleManager>
  </system.web>
</configuration>

Session State provider

The session-state store provider is called by the SessionStateModule class during the processing of an ASP.NET page to communicate with the data store for the storage and retrieval of session variables and related session information such as the time-out value. Session data within each ASP.NET application is stored separately for each SessionID property. ASP.NET applications do not share session data.

For information on how to employ this functionality refer to ASP.NET Session State topic in MSDN. The following example shows the Web.config file for an ASP.NET application configured to use MySqlSessionStateStore.

<configuration>
  <appSettings/>
  <connectionStrings>
    <add name="MySqlServices" connectionString="User Id=root;Host=localhost;Database=Test"/>
  </connectionStrings>
  <system.web>
    <sessionState
      cookieless="true"
      regenerateExpiredSessionId="true"
      mode="Custom"
      customProvider="MySqlSessionProvider">
      <providers>
        <add name="MySqlSessionProvider"
          type="CoreLab.MySql.Web.Providers.MySqlSessionStateStore"
          connectionStringName="MySqlSessionServices"
          writeExceptionsToEventLog="false"/>
      </providers>
    </sessionState>
  </system.web>
</configuration>

Profile provider

The fundamental job of a profile provider is to write profile property values supplied by ASP.NET to persistent profile data sources, and to read the property values back from the data source when requested by ASP.NET.

Unlike Session State provider, Profile provider is a typed structured data storage. It supports both registered and anonymous users. Profile providers also implement methods that allow consumers to manage profile data sources - for example, to delete profiles that haven't been accessed since a specified date.

The user profile is accessed using the Profile property of the current HttpContext object. The following example shows the Web.config file for an ASP.NET application configured to use MySqlProfileProvider.

<configuration>
  <connectionStrings>
    <add name="MySqlServices" connectionString="User Id=root;Host=localhost;Database=Test" />
  </connectionStrings>
  <system.web>
    ...
    <membership defaultProvider="AspNetMySqlMembershipProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add
          name="AspNetMySqlMembershipProvider"
          type="CoreLab.MySql.Web.Providers.MySqlMembershipProvider"
          connectionStringName="MySqlServices"
          enablePasswordRetrieval="false"
          enablePasswordReset="true"
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false"
          passwordFormat="Hashed"
          maxInvalidPasswordAttempts="5"
          passwordAttemptWindow="10" />
      </providers>
    </membership>
    <profile defaultProvider="AspNetMySqlProfileProvider" >
      <providers>
        <add
          name="AspNetMySqlProfileProvider"
          type="CoreLab.MySql.Web.Providers.MySqlProfileProvider"
          connectionStringName="MySqlServices" />
      </providers>
      <properties>
        <add name="ZipCode" />
        <add name="CityAndState" />
      </properties>
    </profile>
  </system.web>
</configuration>

Here ZipCode and CityAndState are examples of profile elements. For detailed information on how to construct properties section refer to MSDN.

Deployment

To deploy ASP.NET applications written with MyDirect .NET you should register run-time assemblies CoreLab.Data.dll, CoreLab.MySql.dll, and CoreLab.MySql.Web.dll at Global Assembly Cache (GAC) for appropriate framework or place it in the folder of your application (Bin folder for web projects).

Also place App_Licenses.dll assembly in the Bin folder. For more information about this assembly refer to Licensing topic.

Keep in mind that web application are usually run in a partially trusted environment. Here are the permissions required by MyDirect .NET:

See Also

DotNetNuke Support

 

 


© 2002-2008 Devart. All rights reserved.