Click here to Skip to main content
6,822,613 members and growing! (16,622 online)
Email Password   helpLost your password?
Database » Database » General     Intermediate

Membership and Role providers for MySQL

By Rakotomalala Andriniaina

This article provides two files that contain a Membership Provider and a Role Provider for ASP.NET v2.0.
C#, Javascript, XML, Windows, .NET, ASP.NET, Visual-Studio, Ajax, MySQL, Architect, Dev
Posted:16 Nov 2005
Updated:20 Dec 2005
Views:351,729
Bookmarked:154 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
62 votes for this article.
Popularity: 8.23 Rating: 4.59 out of 5
1 vote, 1.6%
1
1 vote, 1.6%
2
1 vote, 1.6%
3
6 votes, 9.7%
4
53 votes, 85.5%
5

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

About the Author

Rakotomalala Andriniaina


Member
I am a software developper working for a multinational, in the south of France.
Occupation: Web Developer
Location: France France

Other popular Database articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 212 (Total in Forum: 212) (Refresh)FirstPrevNext
Generalstep 1 error Pinmemberthisismycode1:39 11 Sep '09  
GeneralRe: step 1 error Pinmemberurbluca1:57 16 Oct '09  
GeneralRe: step 1 error PinmemberRuandv21:54 9 Dec '09  
GeneralRecompiling Pinmembersimba2223:26 25 Aug '09  
QuestionCould not load type 'Andri.Web.MySqlRoleProvider'. PinmemberThe_J0ker23:15 8 Jul '09  
Generalhelp me Pinmemberjorgebatista0720:43 10 Jun '09  
GeneralRe: help me PinmemberThe_J0ker23:19 8 Jul '09  
Generalerror Pinmemberjorgebatista0720:38 10 Jun '09  
GeneralRe: error Pinmemberpalmerm15:44 12 Jun '09  
GeneralRe: error Pinmemberpalmerm16:16 12 Jun '09  
QuestionProvider Could Not Establish A Connection to the Database PLEASE HELP1 PinmemberTransMayernik75113:10 6 Apr '09  
AnswerRe: Provider Could Not Establish A Connection to the Database PLEASE HELP1 PinmemberTransMayernik75117:25 6 Apr '09  
Generalcannot handle more than one user Pinmembermam544:17 29 Nov '08  
GeneralRe: cannot handle more than one user Pinmembermam545:29 29 Nov '08  
GeneralNatively supported by connector? Pinmemberlblogic12:46 19 Nov '08  
GeneralAdd new role and add a user in a role Pinmembersemi12:29 2 Nov '08  
GeneralUsing .NET Connector v5.2.3 Pinmemberpalmerm119:16 19 Oct '08  
GeneralProfile Pinmemberuswebpro223:21 2 Sep '08  
General"Could not find any recognizable digits" - error PinmemberJo3p6:08 19 Jun '08  
Generalhow to add custom fields like Firstname etc using Andri Mysql Membership Provider Pinmemberryandemelo8:53 11 Jun '08  
QuestionCan't use this provider to get the currently logged in user Pinmemberandieje112:38 8 May '08  
AnswerRe: Can't use this provider to get the currently logged in user Pinmembervakti9:38 25 May '08  
GeneralHow to provide Custome eror message Pinmemberpodal18:31 24 Apr '08  
GeneralWhy not Navigate to my Request URL Pinmemberpodal17:49 23 Apr '08  
QuestionCookies Time Expires PinmemberMember 33556825:47 3 Apr '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 Dec 2005
Editor: Smitha Vijayan
Copyright 2005 by Rakotomalala Andriniaina
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project