Click here to Skip to main content
15,885,365 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

 
QuestionVWD 2005 or VWD Orcas Pin
Sojan8024-Jun-07 2:39
Sojan8024-Jun-07 2:39 
GeneralminRequiredPasswordLength Pin
MinkyMeerkat24-May-07 1:52
MinkyMeerkat24-May-07 1:52 
QuestionCan creat user but cannot login ! Pin
NaNouk_X21-May-07 10:47
NaNouk_X21-May-07 10:47 
AnswerRe: Can creat user but cannot login ! Pin
MinkyMeerkat24-May-07 13:03
MinkyMeerkat24-May-07 13:03 
GeneralRe: Can creat user but cannot login ! Pin
NaNouk_X30-May-07 3:20
NaNouk_X30-May-07 3:20 
GeneralRe: Can creat user but cannot login ! Pin
Sojan8024-Jun-07 5:40
Sojan8024-Jun-07 5:40 
GeneralRe: Can creat user but cannot login ! Pin
JoeKool6-Jul-07 9:14
JoeKool6-Jul-07 9:14 
GeneralRe: Can creat user but cannot login ! Pin
dustinhorne19-Sep-07 3:54
dustinhorne19-Sep-07 3:54 
GeneralWebsite Administration Tool Pin
b_hardman27-Apr-07 12:42
b_hardman27-Apr-07 12:42 
GeneralRe: Website Administration Tool Pin
GerritJanGerritJan2-Oct-07 22:45
GerritJanGerritJan2-Oct-07 22:45 
GeneralRe: Website Administration Tool Pin
DaveHoganIW22-Nov-07 23:58
DaveHoganIW22-Nov-07 23:58 
GeneralProblema com conexao Pin
Gilberto Pavanelli23-Apr-07 14:44
Gilberto Pavanelli23-Apr-07 14:44 
GeneralRe: Problema com conexao Pin
wdanda25-Aug-07 22:39
wdanda25-Aug-07 22:39 
Questionerror loading Pin
nictmanss20-Apr-07 3:14
nictmanss20-Apr-07 3:14 
GeneralSQL Code didn't work in Navicat for MySQL, here's an update Pin
Veector17-Apr-07 9:36
Veector17-Apr-07 9:36 
GeneralRe: SQL Code didn't work in Navicat for MySQL, here's an update Pin
dustinhorne19-Sep-07 3:56
dustinhorne19-Sep-07 3:56 
GeneralHers is the MySQL 4 SQL Pin
dustinhorne9-Apr-07 6:10
dustinhorne9-Apr-07 6:10 
GeneralRe: Hers is the MySQL 4 SQL Pin
dustinhorne14-Sep-07 4:07
dustinhorne14-Sep-07 4:07 
QuestionRe: Hers is the MySQL 4 SQL Pin
josepaulino30-Sep-07 18:32
josepaulino30-Sep-07 18:32 
AnswerRe: Hers is the MySQL 4 SQL Pin
josepaulino30-Sep-07 18:38
josepaulino30-Sep-07 18:38 
GeneralHere is the VB Version Pin
dustinhorne9-Apr-07 5:53
dustinhorne9-Apr-07 5:53 
GeneralRe: Here is the VB Version Pin
Zyndrof26-Apr-07 6:49
Zyndrof26-Apr-07 6:49 
AnswerRe: Here is the VB Version Pin
dustinhorne30-Apr-07 10:24
dustinhorne30-Apr-07 10:24 
GeneralRe: Here is the VB Version Pin
dustinhorne14-Sep-07 4:06
dustinhorne14-Sep-07 4:06 
QuestionHow to create login session? Pin
Maysoon_Ahmad2-Apr-07 11:14
Maysoon_Ahmad2-Apr-07 11:14 

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.