Click here to Skip to main content
Licence CPOL
First Posted 7 Sep 2007
Views 72,502
Downloads 500
Bookmarked 64 times

MySQL Membership, Role, Sitemap, Personalization Provider for ASP.NET 2.0

By | 7 Sep 2007 | Article
A Membership, Role, Sitemap, and Personalization provider using MySQL.
 
Part of The SQL Zone sponsored by
See Also

Introduction

This is the implementation of a Membership, Role, Sitemap, and Personalization provider of ASP.NET using MySQL as the backend database. This class uses the native net MySQL Connector version 5.1.2.

Background

This code is based on the work of J Snyman; he used ODBC to connect to MySQL. I modified it to use the native net MySQL Connector version 5.1.2.

Using the code

This is actually a C# class project. You only need to reference the compiled DLL output from this project in your Web project. You can use this even if the language in your web app is VB. The provided compiled DLL can be used right out of the box, but if you prefer, you can compile your own.

Using the providers is really easy.

  1. Create a new database on your MySQL server, e.g.: SimpleProviders.
  2. Execute the following SQL statement on the newly created database:
  3. CREATE TABLE 'personalization' (
    'username' varchar(255) default NULL,
    'path' varchar(255) default NULL,
    'applicationname' varchar(255) default NULL,
    'personalizationblob' blob
    );
    
    CREATE TABLE 'profiles' (
    'UniqueID' int(8) NOT NULL auto_increment,
    'Username' varchar(255) NOT NULL default '',
    'ApplicationName' varchar(255) NOT NULL default '',
    'IsAnonymous' tinyint(1) default '0',
    'LastActivityDate' datetime default NULL,
    'LastUpdatedDate' datetime default NULL,
    PRIMARY KEY ('UniqueID'),
    UNIQUE KEY 'PKProfiles' ('Username','ApplicationName'),
    UNIQUE KEY 'PKID' ('UniqueID')
    );
    
    CREATE TABLE 'roles' (
    'Rolename' varchar(255) NOT NULL default '',
    'ApplicationName' varchar(255) NOT NULL default '',
    PRIMARY KEY ('Rolename','ApplicationName')
    );
    
    CREATE TABLE 'sitemap' (
    'ID' int(11) NOT NULL auto_increment,
    'ApplicationName' varchar(255) NOT NULL default '',
    'Title' varchar(255) default NULL,
    'Description' text,
    'Url' text,
    'Roles' text,
    'Parent' int(11) default NULL,
    PRIMARY KEY ('ID')
    );
    
    CREATE TABLE 'users' (
    'PKID' varchar(255) NOT NULL default '',
    'Username' varchar(255) NOT NULL default '',
    'ApplicationName' varchar(255) NOT NULL default '',
    'Email' varchar(128) default NULL,
    'Comment' varchar(255) default NULL,
    'Password' varchar(128) NOT NULL default '',
    'FailedPasswordAttemptWindowStart' datetime default NULL,
    'PasswordQuestion' varchar(255) default NULL,
    'IsLockedOut' tinyint(1) default '0',
    'PasswordAnswer' varchar(255) default NULL,
    'FailedPasswordAnswerAttemptCount' int(8) default '0',
    'FailedPasswordAttemptCount' int(8) default '0',
    'IsApproved' tinyint(1) NOT NULL default '0',
    'FailedPasswordAnswerAttemptWindowStart' datetime default NULL,
    'LastActivityDate' datetime default NULL,
    'IsOnLine' tinyint(1) default '0',
    'CreationDate' datetime default NULL,
    'LastPasswordChangedDate' datetime default NULL,
    'LastLockedOutDate' datetime default NULL,
    'LastLoginDate' datetime default NULL,
    PRIMARY KEY ('PKID'),
    UNIQUE KEY 'PKID' ('PKID'),
    KEY 'PKID_2' ('PKID'),
    KEY 'usr' ('Username')
    );
    
    CREATE TABLE 'usersinroles' (
    'Username' varchar(255) NOT NULL default '',
    'Rolename' varchar(255) NOT NULL default '',
    'ApplicationName' varchar(255) NOT NULL default '',
    PRIMARY KEY ('Username','Rolename','ApplicationName')
    );

    There is a SQL file named DBStructure.sql included with the source code zip file that contains the code above.

  4. Open Visual Studio and create a new Website Project.
  5. Add a reference to Simple.Providers.MySQL.dll.
  6. Make the following changes to your web.config file:
    1. Add the connection string to your newly created database to the connectionStrings section:
    2. <add 
        connectionString="server=localhost;database=simpleproviders;
                          user id=<put user>;pwd=<put password>" 
        name="SimpleProviderconnectionstring" 
        providerName="MySql.Data.MySqlClient"/>

      * Please replace the {Your username} and {Your password} entries in the connection string with your own values.

    3. Under the <system.web> section, add the following:
    4. <siteMap defaultProvider="siteMapProvider" enabled="true">
        <providers>
          <clear />
           <add name="siteMapProvider" 
             type="Simple.Providers.MySQL.MysqlSiteMapProvider" 
             connectionStringName="SimpleProviderConnectionString" 
             applicationName="{Your App Name}" 
             description="MySQL site map provider" 
             securityTrimmingEnabled="true"/>
        </providers>
      </siteMap>
      <roleManager defaultProvider="roleProvider" enabled="true" 
             cacheRolesInCookie="false" cookieName=".ASPROLES" 
             cookieTimeout="7200" cookiePath="/" cookieRequireSSL="false" 
             cookieSlidingExpiration="true" cookieProtection="All">
        <providers>
        <clear />
        <add name="roleProvider" 
          type="Simple.Providers.MySQL.MysqlRoleProvider" 
          connectionStringName="SimpleProviderConnectionString" 
          applicationName="{Your App Name}" 
          description="MySQL role provider"/>
        </providers>
      </roleManager>
      <membership defaultProvider="membershipProvider" 
            userIsOnlineTimeWindow="15">
        <providers>
        <clear />
        <add name="membershipProvider" 
          type="Simple.Providers.MySQL.MysqlMembershipProvider" 
          connectionStringName="SimpleProviderConnectionString" 
          applicationName="{Your App Name}" 
          enablePasswordRetrieval="true" enablePasswordReset="true" 
          requiresQuestionAndAnswer="true" 
          requiresUniqueEmail="true" passwordFormat="Encrypted" 
          minRequiredPasswordLength="6" 
          minRequiredNonalphanumericCharacters="0" 
          description="MySQL membership provider"/>
        </providers>
      </membership>
      <profile defaultProvider="profileProvider" 
              automaticSaveEnabled="true">
        <providers>
        <clear />
        <add name="profileProvider" 
          type="Simple.Providers.MySQL.MysqlProfileProvider" 
          connectionStringName="SimpleProviderConnectionString" 
          applicationName="{Your App Name}" 
          description="MySQL Profile Provider"/>
        </providers>
        <properties>
        <clear />
          <!--
            Add any needed attributes for profiles here.
            eg. <add name="Theme" type="System.String" 
            defaultValue="Default"/>
          -->
        </properties>
      </profile>
      <webParts>
        <personalization defaultProvider="personalizationProvider">
        <providers>
          <clear />
          <add name="personalizationProvider" 
             type="Simple.Providers.MySQL.MysqlPersonalizationProvider" 
             connectionStringName="{Your Connection String Name}" 
             applicationName="{Your App Name}" 
             description="MySQL Personalization Provider/>
          </providers>
        </personalization>
      </webParts>

      Please replace the {Your App Name} instances with a valid application name. The application name should not contain any spaces or special characters.

  7. Everything should be set up correctly now.

Continue with the rest of your project and make sure to make use of the features provided by the above mentioned providers.

History

  • 9/7/2007 - Uses the MySQL Net Connector 5.1.2 instead of ODBC.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

onrac

Web Developer

Philippines Philippines

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhelp me remove this error. Pinmembervirus231:25 2 Feb '10  
QuestionHow do the Simple Providers recognize the current version of MySQL Connector Driver PinmemberLuizItatiba17:34 8 Sep '09  
QuestionRe: Webparts PinmemberMember 411560413:27 2 Apr '09  
QuestionHow CurrentNodes and accented with spaces in SiteMapPath? For example São Paulo-SP. PinmemberLuizItatiba9:25 22 Oct '08  
QuestionHow to correctly set the standard MemberShipProvider Provider of the SqlServer2005 to work together with the MySqlSiteMapProvider? PinmemberLuizItatiba6:26 8 Oct '08  
QuestionHow can I use same database for more than one application? PinmemberUmut TURER6:39 2 Sep '08  
QuestionCan someone provide the same example with some aspx pages..... PinmemberMember 358658023:20 15 Jul '08  
Generalgood PinmemberDidier Stmedar3:33 4 Jul '08  
QuestionFail to test to the provider in the Asp.net Configuration PinmemberCGARRIGOS1:54 20 May '08  
QuestionHow to Order Alphabetically by the children MySqlSiteMapProvider us PinmemberLuizItatiba16:30 1 Apr '08  
QuestionAs Custom Menus of MYSQLSITEMAPPROVIDER, based on profiles or roles PinmemberLuizItatiba9:24 1 Apr '08  
QuestionError Tool Administration of Site or ASP.NET Configuration PinmemberLuizItatiba8:57 1 Apr '08  
QuestionError WebConfig PinmemberLuizItatiba10:18 31 Mar '08  
QuestionUsing the driver MySqlConnector 5.0 instead of 5.1.2 PinmemberLuizItatiba12:44 26 Mar '08  
GeneralGeneral Comments PinmvpJohn Simmons / outlaw programmer4:33 19 Jan '08  
Thanks for doing this, but...

1) Your code formatting is, for lack of a better term, CRAP. If you're going to essentially duplicate someone else's code, at least go through it to the point that it looks decent.
 
2) You need to update the code to eliminate the deprecated MySqlDataType.Datetime references.
 
3) Both your version and the original contain an error in the web.config stuff. The very last item has the ending quote in the wrong place. It seems to me that one of you guys (you or the guy that posted the original article) would have picked up on it by now.
 
4) You need to go back through all the source code and add proper intellisense-compatible comments to all of the classes, properties and functions.
 

"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

QuestionODBC still present in the MysqlPersonalizationProvider.cs PinmemberLisburn Lad4:01 8 Jan '08  
AnswerRe: ODBC still present in the MysqlPersonalizationProvider.cs Pinmemberluciano vilela4:20 26 May '10  
GeneralImplementation Example PinmemberCreativeGolf5:54 19 Oct '07  
Generaljust Note Pinmemberibyasmo10:45 30 Sep '07  
QuestionERROR MESSAGE - SECURITY PinmemberKanedogg7:48 17 Sep '07  
AnswerRe: ERROR MESSAGE - SECURITY [modified] Pinmemberonrac17:18 17 Sep '07  
GeneralRe: ERROR MESSAGE - SECURITY PinmemberKanedogg0:47 18 Sep '07  
QuestionRe: MySQL Membership, Role Provider [modified] PinmemberFuzziebrain15:23 13 Sep '07  
AnswerRe: MySQL Membership, Role Provider Pinmemberonrac17:20 17 Sep '07  
Generalgood effort Pinmemberlxwde14:52 10 Sep '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 8 Sep 2007
Article Copyright 2007 by onrac
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid