Click here to Skip to main content
12,830,888 members (47,648 online)
Click here to Skip to main content
Add your own
alternative version


177 bookmarked
Posted 16 Nov 2005

Membership and Role providers for MySQL

, 20 Dec 2005
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.


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).
      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,
    ) 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):
        <add name="ConnString" 
          connectionString="Database=YOURDBNAME;Data Source=localhost;
                            User Id=YOURUSERNAME;Password=YOURPWD" />
        <roleManager defaultProvider="MySqlRoleProvider"
            cookieProtection="All" >
            <clear />
        <membership defaultProvider="MySqlMembershipProvider" 
                <clear />
  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?


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
Web Developer
France France
I am a software developper working for a multinational, in the south of France.

You may also be interested in...


Comments and Discussions

QuestionNo Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'. Pin
Member 1154109422-Mar-15 9:59
memberMember 1154109422-Mar-15 9:59 
GeneralNice work Pin
thardes210-Sep-13 2:56
memberthardes210-Sep-13 2:56 
QuestionCould not load type 'Andri.Web.MySqlRoleProvider' Pin
elektrastallion14-Aug-13 19:32
memberelektrastallion14-Aug-13 19:32 
QuestionUnable to connect to any of the specified MySQL hosts. Pin
kish_1238-Nov-12 19:28
memberkish_1238-Nov-12 19:28 
GeneralMy vote of 5 Pin
谢华梁14-May-12 21:10
member谢华梁14-May-12 21:10 
QuestionProblem with IIS 7 Pin
titimanu6922-Nov-11 7:01
membertitimanu6922-Nov-11 7:01 
QuestionCould not load type 'MySql.Web.Security.MySQLRoleProvider'. Pin
Scottj196824-Aug-11 11:34
memberScottj196824-Aug-11 11:34 
AnswerRe: Could not load type 'MySql.Web.Security.MySQLRoleProvider'. Pin
koray451-Apr-12 5:53
memberkoray451-Apr-12 5:53 
Questionhow to add custom fields like Firstname , sex etc using Andri Mysql Membership Provider Pin
gavinv28-Jan-11 23:18
membergavinv28-Jan-11 23:18 
GeneralMy vote of 5 Pin
JoseGzzV14-Sep-10 13:23
memberJoseGzzV14-Sep-10 13:23 
GeneralSolution if your login control doesnt work when you deploy the application Pin
hvemmig12324-May-10 4:03
memberhvemmig12324-May-10 4:03 
GeneralNeed help with error System.Security.SecurityException: That assembly does not allow partially trusted callers. Pin
jbumgarden25-Mar-10 19:31
memberjbumgarden25-Mar-10 19:31 
Here is a little background regarding my issue. I'm running an website using vb coding. I have implemented your mysql membership provider utilizing the latest mysql .net connector v6.3. I can run the project fine on my local computer with no problems but the moment i upload it to my Godaddy Windows Shared hosting i get this error below. Please advise. Thanks in advance for your help.


Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: That assembly does not allow partially trusted callers.

Source Error:

Line 1293:
Line 1294: return isValid;
Line 1295: }
Line 1296:
Line 1297:

Source File: d:\hosting\4622657\html\Development-Websites\Threads-N-Inks\App_Code\MySQLMembershipProvider.cs Line: 1295

Stack Trace:

[SecurityException: That assembly does not allow partially trusted callers.]
Andri.Web.MySqlMembershipProvider.ValidateUser(String username, String password) in d:\hosting\4622657\html\Development-Websites\Threads-N-Inks\App_Code\MySQLMembershipProvider.cs:1295
System.Web.UI.WebControls.Login.AuthenticateUsingMembershipProvider(AuthenticateEventArgs e) +60
System.Web.UI.WebControls.Login.OnAuthenticate(AuthenticateEventArgs e) +119
System.Web.UI.WebControls.Login.AttemptLogin() +115
System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +101
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.login_aspx.ProcessRequest(HttpContext context) +37
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
GeneralFree ASP.Net Hosting Pin
kotogasy16-Mar-10 1:05
memberkotogasy16-Mar-10 1:05 
GeneralThank you Pin
Bruno921306-Mar-10 5:31
memberBruno921306-Mar-10 5:31 
Generalstep 1 error Pin
thisismycode11-Sep-09 1:39
memberthisismycode11-Sep-09 1:39 
GeneralRe: step 1 error Pin
urbluca16-Oct-09 1:57
memberurbluca16-Oct-09 1:57 
GeneralRe: step 1 error Pin
Ruandv9-Dec-09 21:54
memberRuandv9-Dec-09 21:54 
GeneralRecompiling Pin
simba22225-Aug-09 3:26
membersimba22225-Aug-09 3:26 
QuestionCould not load type 'Andri.Web.MySqlRoleProvider'. Pin
The_J0ker8-Jul-09 23:15
memberThe_J0ker8-Jul-09 23:15 
Generalhelp me Pin
jorgebatista0710-Jun-09 20:43
memberjorgebatista0710-Jun-09 20:43 
GeneralRe: help me Pin
The_J0ker8-Jul-09 23:19
memberThe_J0ker8-Jul-09 23:19 
Generalerror Pin
jorgebatista0710-Jun-09 20:38
memberjorgebatista0710-Jun-09 20:38 
GeneralRe: error Pin
palmerm112-Jun-09 5:44
memberpalmerm112-Jun-09 5:44 
GeneralRe: error Pin
palmerm112-Jun-09 6:16
memberpalmerm112-Jun-09 6:16 
QuestionProvider Could Not Establish A Connection to the Database PLEASE HELP1 Pin
TransMayernik7516-Apr-09 13:10
memberTransMayernik7516-Apr-09 13:10 

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.

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