Click here to Skip to main content
15,902,112 members
Articles / Web Development / ASP.NET
Article

Membership and Role providers for MySQL

Rate me:
Please Sign up or sign in to vote.
4.86/5 (71 votes)
20 Dec 20051 min read 1M   5.8K   178   227
This article provides two files that contain a Membership Provider and a Role Provider for ASP.NET v2.0.

Introduction

This article provides two files that contain a Membership provider and a Role provider for ASP.NET v2.0.

Microsoft provides a Membership provider in the framework but only for SQL Server. The class does not seem to work for MySQL so I decided to write a new provider from the ODBC provider sample code included in the framework SDK.

How to use it

To use these classes, you will need the latest MySQL .NET Connector. You might need to recompile it with the new C# compiler for v2.0 (but the new framework should normally support v1.0 or 1.1 assemblies).

  1. Create the tables (note that these SQL commands might not work with MySQL 4).
    SQL
    CREATE TABLE Roles
    (
      Rolename Varchar (255) NOT NULL,
      ApplicationName varchar (255) NOT NULL
    )
    
    CREATE TABLE UsersInRoles
    (
      Username Varchar (255) NOT NULL,
      Rolename Varchar (255) NOT NULL,
      ApplicationName Text (255) NOT NULL
    )
    ALTER TABLE 'usersinroles' 
          ADD INDEX ( 'Username', 'Rolename', 'ApplicationName') ;
    ALTER TABLE 'roles' ADD INDEX ( 'Rolename' , 'ApplicationName' ) ;
    
    CREATE TABLE 'users' (
      'PKID' varchar(36) collate latin1_general_ci NOT NULL default '',
      'Username' varchar(255) collate latin1_general_ci NOT NULL default '',
      'ApplicationName' varchar(100) 
                        collate latin1_general_ci NOT NULL default '',
      'Email' varchar(100) collate latin1_general_ci NOT NULL default '',
      'Comment' varchar(255) collate latin1_general_ci default NULL,
      'Password' varchar(128) collate latin1_general_ci NOT NULL default '',
      'PasswordQuestion' varchar(255) collate latin1_general_ci default NULL,
      'PasswordAnswer' varchar(255) collate latin1_general_ci default NULL,
      'IsApproved' tinyint(1) default NULL,
      'LastActivityDate' datetime default NULL,
      'LastLoginDate' datetime default NULL,
      'LastPasswordChangedDate' datetime default NULL,
      'CreationDate' datetime default NULL,
      'IsOnLine' tinyint(1) default NULL,
      'IsLockedOut' tinyint(1) default NULL,
      'LastLockedOutDate' datetime default NULL,
      'FailedPasswordAttemptCount' int(11) default NULL,
      'FailedPasswordAttemptWindowStart' datetime default NULL,
      'FailedPasswordAnswerAttemptCount' int(11) default NULL,
      'FailedPasswordAnswerAttemptWindowStart' datetime default NULL,
      PRIMARY KEY  ('PKID')
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci;
  2. Change MySqlMembershipProvider::encryptionKey to a random hexadecimal value of your choice.
  3. Upload the two files to ~/App_Code.
  4. Modify your web.config using the following template (if you are on a shared hosting server, you will have to set writeExceptionsToEventLog to false):
    XML
    <connectionStrings>
        <add name="ConnString" 
          connectionString="Database=YOURDBNAME;Data Source=localhost;
                            User Id=YOURUSERNAME;Password=YOURPWD" />
    </connectionStrings>
    <system.web>
        <roleManager defaultProvider="MySqlRoleProvider"
            enabled="true"
            cacheRolesInCookie="true"
            cookieName=".ASPROLES"
            cookieTimeout="30"
            cookiePath="/"
            cookieRequireSSL="false"
            cookieSlidingExpiration="true"
            cookieProtection="All" >
        <providers>
            <clear />
            <add
                name="MySqlRoleProvider"
                type="Andri.Web.MySqlRoleProvider"
                connectionStringName="ConnString"
                applicationName="YOURAPPNAME"
                writeExceptionsToEventLog="true"
            />
        </providers>
        </roleManager>
        
        <membership defaultProvider="MySqlMembershipProvider" 
                    userIsOnlineTimeWindow="15">
            <providers>
                <clear />
                <add
                    name="MySqlMembershipProvider"
                    type="Andri.Web.MySqlMembershipProvider"
                    connectionStringName="ConnString"
                    applicationName="YOURAPPNAME"
                    enablePasswordRetrieval="false"
                    enablePasswordReset="true"
                    requiresQuestionAndAnswer="true"
                    requiresUniqueEmail="true"
                    passwordFormat="Hashed"
                    writeExceptionsToEventLog="true"
                />
          </providers>
        </membership>
    </system.web>
  5. That's it! You can use the login controls provided in the ASP.NET framework v2.0 if you want or use your own controls.

You can do whatever you want with this code but just send me an email if you have done some improvements or some bug correction. Isn't that what this website is about, sharing?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
France France
I am a software developper working for a multinational, in the south of France.

Comments and Discussions

 
GeneralMySQL Connector/Net 5.2.1 [modified] Pin
nolan3-Mar-08 16:10
nolan3-Mar-08 16:10 
GeneralRe: MySQL Connector/Net 5.2.1 Pin
Matthew Clemmons21-Mar-08 14:16
Matthew Clemmons21-Mar-08 14:16 
GeneralRe: MySQL Connector/Net 5.2.1 Pin
fooboo_025-Jul-08 5:29
fooboo_025-Jul-08 5:29 
QuestionIs it just me? Pin
Mike Codey24-Feb-08 5:31
Mike Codey24-Feb-08 5:31 
AnswerRe: Is it just me? Pin
nafster16-Apr-08 2:05
nafster16-Apr-08 2:05 
QuestionWhat about odbc and mysql? Pin
Reader Tommy12-Feb-08 6:52
Reader Tommy12-Feb-08 6:52 
GeneralIf Locked need to display an new custom messge [modified] Pin
Member 408285327-Jan-08 19:39
Member 408285327-Jan-08 19:39 
GeneralConfiguration Error Pin
haniel3-Jan-08 13:38
haniel3-Jan-08 13:38 
Hi,

I have used the work created by Andri (thanks Andri Laugh | :laugh: ). But when I uploaded the files to the server I got the following error:



Configuration Error

Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: Could not load type 'Andri.Web.MySqlRoleProvider'.

Source Error:

Line 92:                 <add><br />
Line 93:                     name="MySqlRoleProvider"<br />
Line 94:                     type="Andri.Web.MySqlRoleProvider"<br />
Line 95:                     connectionStringName="ConnString"<br />
Line 96:                     applicationName="Skillconnect"</add>



Source File: C:\Domains\techniel.com\wwwroot\web.config Line: 94

Version Information: Microsoft .NET Framework Version:2.0.50727.832; ASP.NET Version:2.0.50727.832

Any suggestions on how I can resolve it?

Thanks,
-Haniel
GeneralRe: Configuration Error Pin
buiductien4-Jan-08 16:25
buiductien4-Jan-08 16:25 
GeneralRe: Configuration Error Pin
Gary Loftus15-Mar-08 6:03
Gary Loftus15-Mar-08 6:03 
GeneralRe: Configuration Error Pin
Hawkmoth6-Aug-08 1:59
Hawkmoth6-Aug-08 1:59 
QuestionHow to achieve aspnet2.0 framework to not to create ASPNETDB.MDF file in the APP_DATA directory Pin
Member 22998228-Dec-07 8:23
Member 22998228-Dec-07 8:23 
Questioncompiling the providers Pin
ingresman13-Dec-07 0:47
ingresman13-Dec-07 0:47 
GeneralDon't forget the GO Pin
TyKie11-Dec-07 17:19
TyKie11-Dec-07 17:19 
GeneralProper SQL code for databases Pin
Bursh.24-Nov-07 1:29
Bursh.24-Nov-07 1:29 
GeneralRe: Proper SQL code for databases Pin
Tinkering Turtle12-Mar-08 6:03
Tinkering Turtle12-Mar-08 6:03 
GeneralRe: Proper SQL code for databases Pin
powder8powder16-Jun-08 1:48
powder8powder16-Jun-08 1:48 
GeneralThanks and Help Pin
bill.buckley11-Nov-07 15:54
bill.buckley11-Nov-07 15:54 
GeneralRe: Thanks and Help Pin
box86rowh14-Nov-07 7:56
box86rowh14-Nov-07 7:56 
GeneralRe: Thanks and Help Pin
bill.buckley14-Nov-07 15:23
bill.buckley14-Nov-07 15:23 
GeneralRe: Thanks and Help Pin
orinoco7727-Jul-08 8:09
orinoco7727-Jul-08 8:09 
GeneralRe: Thanks and Help Pin
thardes29-Sep-13 23:31
thardes29-Sep-13 23:31 
Questioncan somebody speak german? Pin
as120825-Oct-07 8:08
as120825-Oct-07 8:08 
GeneralMySqlMembershipProvider functions Pin
marvc116-Oct-07 4:35
marvc116-Oct-07 4:35 
GeneralWeb Site Administration Tool error Pin
AndersLind6-Aug-07 0:58
AndersLind6-Aug-07 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.