Click here to Skip to main content
11,501,968 members (65,932 online)
Click here to Skip to main content

Membership and Role providers for MySQL

, 20 Dec 2005 685.6K 5.2K 177
Rate this:
Please Sign up or sign in to vote.
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).
    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):
    <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

Share

About the Author

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

Comments and Discussions

 
QuestionNo Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'. [modified] Pin
Member 1154109422-Mar-15 8:59
memberMember 1154109422-Mar-15 8:59 
GeneralNice work Pin
thardes210-Sep-13 1:56
memberthardes210-Sep-13 1:56 
QuestionCould not load type 'Andri.Web.MySqlRoleProvider' Pin
elektrastallion14-Aug-13 18:32
memberelektrastallion14-Aug-13 18:32 
QuestionUnable to connect to any of the specified MySQL hosts. Pin
kish_1238-Nov-12 18:28
memberkish_1238-Nov-12 18:28 
GeneralMy vote of 5 Pin
谢华梁14-May-12 20:10
member谢华梁14-May-12 20:10 
QuestionProblem with IIS 7 Pin
titimanu6922-Nov-11 6:01
membertitimanu6922-Nov-11 6:01 
QuestionCould not load type 'MySql.Web.Security.MySQLRoleProvider'. Pin
Scottj196824-Aug-11 10:34
memberScottj196824-Aug-11 10:34 
AnswerRe: Could not load type 'MySql.Web.Security.MySQLRoleProvider'. Pin
koray451-Apr-12 4:53
memberkoray451-Apr-12 4:53 
Questionhow to add custom fields like Firstname , sex etc using Andri Mysql Membership Provider Pin
gavinv28-Jan-11 22:18
membergavinv28-Jan-11 22:18 
GeneralMy vote of 5 Pin
JoseGzzV14-Sep-10 12:23
memberJoseGzzV14-Sep-10 12:23 
GeneralSolution if your login control doesnt work when you deploy the application Pin
hvemmig12324-May-10 3:03
memberhvemmig12324-May-10 3:03 
GeneralNeed help with error System.Security.SecurityException: That assembly does not allow partially trusted callers. Pin
jbumgarden25-Mar-10 18:31
memberjbumgarden25-Mar-10 18:31 
GeneralFree ASP.Net Hosting Pin
kotogasy16-Mar-10 0:05
memberkotogasy16-Mar-10 0:05 
GeneralThank you Pin
Bruno921306-Mar-10 4:31
memberBruno921306-Mar-10 4:31 
Generalstep 1 error Pin
thisismycode11-Sep-09 0:39
memberthisismycode11-Sep-09 0:39 
GeneralRe: step 1 error Pin
urbluca16-Oct-09 0:57
memberurbluca16-Oct-09 0:57 
GeneralRe: step 1 error Pin
Ruandv9-Dec-09 20:54
memberRuandv9-Dec-09 20:54 
GeneralRecompiling Pin
simba22225-Aug-09 2:26
membersimba22225-Aug-09 2:26 
QuestionCould not load type 'Andri.Web.MySqlRoleProvider'. Pin
The_J0ker8-Jul-09 22:15
memberThe_J0ker8-Jul-09 22:15 
Generalhelp me Pin
jorgebatista0710-Jun-09 19:43
memberjorgebatista0710-Jun-09 19:43 
GeneralRe: help me Pin
The_J0ker8-Jul-09 22:19
memberThe_J0ker8-Jul-09 22:19 
Generalerror Pin
jorgebatista0710-Jun-09 19:38
memberjorgebatista0710-Jun-09 19:38 
GeneralRe: error Pin
palmerm112-Jun-09 4:44
memberpalmerm112-Jun-09 4:44 
GeneralRe: error Pin
palmerm112-Jun-09 5:16
memberpalmerm112-Jun-09 5:16 
QuestionProvider Could Not Establish A Connection to the Database PLEASE HELP1 Pin
TransMayernik7516-Apr-09 12:10
memberTransMayernik7516-Apr-09 12:10 
AnswerRe: Provider Could Not Establish A Connection to the Database PLEASE HELP1 Pin
TransMayernik7516-Apr-09 16:25
memberTransMayernik7516-Apr-09 16:25 
Generalcannot handle more than one user Pin
mam5429-Nov-08 3:17
membermam5429-Nov-08 3:17 
GeneralRe: cannot handle more than one user Pin
mam5429-Nov-08 4:29
membermam5429-Nov-08 4:29 
QuestionNatively supported by connector? Pin
lblogic19-Nov-08 11:46
memberlblogic19-Nov-08 11:46 
GeneralAdd new role and add a user in a role Pin
semi2-Nov-08 11:29
membersemi2-Nov-08 11:29 
GeneralUsing .NET Connector v5.2.3 Pin
palmerm119-Oct-08 18:16
memberpalmerm119-Oct-08 18:16 
GeneralProfile Pin
uswebpro22-Sep-08 22:21
memberuswebpro22-Sep-08 22:21 
General"Could not find any recognizable digits" - error Pin
Jo3p19-Jun-08 5:08
memberJo3p19-Jun-08 5:08 
Questionhow to add custom fields like Firstname etc using Andri Mysql Membership Provider Pin
ryandemelo11-Jun-08 7:53
memberryandemelo11-Jun-08 7:53 
QuestionCan't use this provider to get the currently logged in user Pin
andieje18-May-08 11:38
memberandieje18-May-08 11:38 
QuestionHow to provide Custome eror message Pin
podal24-Apr-08 17:31
memberpodal24-Apr-08 17:31 
GeneralWhy not Navigate to my Request URL Pin
podal23-Apr-08 16:49
memberpodal23-Apr-08 16:49 
QuestionCookies Time Expires Pin
Member 33556823-Apr-08 4:47
memberMember 33556823-Apr-08 4:47 
QuestionHow to use this Role provider for Oracle database Pin
elizabethsheeba11-Mar-08 5:36
memberelizabethsheeba11-Mar-08 5:36 
GeneralMySQL Connector/Net 5.2.1 [modified] Pin
nolan3-Mar-08 16:10
membernolan3-Mar-08 16:10 
GeneralRe: MySQL Connector/Net 5.2.1 Pin
Matthew Clemmons21-Mar-08 14:16
memberMatthew Clemmons21-Mar-08 14:16 
GeneralRe: MySQL Connector/Net 5.2.1 Pin
fooboo_025-Jul-08 5:29
memberfooboo_025-Jul-08 5:29 
QuestionIs it just me? Pin
Mike Codey24-Feb-08 5:31
memberMike Codey24-Feb-08 5:31 
AnswerRe: Is it just me? Pin
nafster16-Apr-08 2:05
membernafster16-Apr-08 2:05 
QuestionWhat about odbc and mysql? Pin
Reader Tommy12-Feb-08 6:52
memberReader Tommy12-Feb-08 6:52 
GeneralIf Locked need to display an new custom messge [modified] Pin
Member 408285327-Jan-08 19:39
memberMember 408285327-Jan-08 19:39 
GeneralConfiguration Error Pin
haniel3-Jan-08 13:38
memberhaniel3-Jan-08 13:38 
GeneralRe: Configuration Error Pin
buiductien4-Jan-08 16:25
memberbuiductien4-Jan-08 16:25 
GeneralRe: Configuration Error Pin
Gary Loftus15-Mar-08 6:03
memberGary Loftus15-Mar-08 6:03 
GeneralRe: Configuration Error Pin
Hawkmoth6-Aug-08 1:59
memberHawkmoth6-Aug-08 1:59 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 20 Dec 2005
Article Copyright 2005 by Rakotomalala Andriniaina
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid