Click here to Skip to main content
15,879,184 members
Articles / Web Development / ASP.NET
Article

C# MySQL Profile Provider for .NET 2.0

Rate me:
Please Sign up or sign in to vote.
3.17/5 (15 votes)
13 Sep 20063 min read 217.4K   958   66   64
This is a MySQL based Profile Provider written in C# for .NET 2.0

Introduction

This is about a MySql Profile Provider.

I did not write the profile provider from scratch. I used a sample Microsoft Access profile provider from the Microsoft SDK, and modified it to work with MySQL.

If you are new to the .NET 2.0 Provider technology, then refer to this article to learn about how the Membership and Role provider works. Membership and Role providers for MySQL by Rakotomalala Andriniaina.

Note: This profile provider was written to work with a specific MySQL Membership and Role Provider which is from another codeproject article. This profile provider will not work unless the membership and role providers are setup and working! The Source download for this article has been updated to include the MYSQL Role,Membership, and Profile provider source.

If you are new to the .NET 2.0 Provider technology, then refer to this article to learn about how the Membership and Role provider works. However, do not use the source from the link below because the code has been updated in the link above: Membership and Role providers for MySQL by Rakotomalala Andriniaina.

Using the Provider

Step 1: Add two new tables, aspnet_profile and aspnet_applications.

SQL
CREATE TABLE 'aspnet_profile' (
  'UserId' bigint(20) default NULL,
  'PropertyNames' text,
  'PropertyValuesString' text,
  'LastUpdatedDate' datetime default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE 'aspnet_applications' (
  'ApplicationName' text,
  'ApplicationId' int(11) NOT NULL auto_increment,
  'Description' text,
  PRIMARY KEY  ('ApplicationId')
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Step 2: Add the MySqlProfileProvider.cs source file to the app_code of the project. If you are using VB.NET, then you have two options:

  • Add a C# class Library project to your solution. Then, add the MySqlProfileProvider.cs source file to the class library project. Add the following references: MySql.Data, System.configuration, and System.Web. Next, add a project reference that points to the new class library project you just added to your original project that will be using the profile provider. When you compile the solution, the C# class library project will compile first and create the DLL, because it is being referenced by the main project. This method will allow you to debug the C# code with your main project even if it is in VB.NET.

    Note: You can also use this method for your membership and role provider source. Just add a separate C# class library project for each provider source file.

  • You will need to compile the C# source into a DLL and then add the reference to your project. Here is a link (Membership and Role providers for MySQL) to the message that will explain compiling the source into a DLL for VB.NET.

Step 3: Modify the Web.config.

XML
<connectionStrings>
 <add name="ConnString" 
      connectionString="Database=;DataSource=;User Id=;Password=;"/>
</connectionStrings>

<profile defaultProvider="MySqlProfileProvider">
  <providers>
    <add name="MySqlProfileProvider" 
         type="Malachi.MySqlProviders.MySqlProfileProvider"
         connectionStringName="ConnString" 
         applicationName="Intranet"/>
  </providers>
  <properties>
    <add name="FirstName" />
    <add name="LastName" />
  </properties>
</profile>

Step 4: Create users.

In order for this profile provider to work, you must use the membership provider from the above mentioned article and create users through that membership provider. A profile provider is an extension of the user. The profile property records are linked to the users table via the UserId field. The UserId field should be auto incremented so it is populated when the user is created. If you would like to populate and create profile properties when the user is created, then you will need to override the ASP.NET control "CreateUserWizard". I will try to write another article soon regarding how to do that. Otherwise, use the methods in the next step in the page loads or other events.

Step 5: Read and write profile properties.

Here is the login page code-behind. This example is in VB.NET.

VB.NET
Protected Sub Page_Load(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles Me.Load
    If User.Identity.IsAuthenticated Then
        IntranetLogin.Visible = False
        Profile.GetProfile(User.Identity.Name)
        lblWelcome.Text = "<p><strong>Welcome back " & _
                          Profile.FirstName & " " & Profile.LastName & _
                          "</strong></p><p>Applications " & _ 
                          "are available from the menu on the right side."
    Else
        lblWelcome.Text = "<p>Login or Create a user account to " & _ 
                          "access Intranet applications.</p><p>" & _ 
                          "Quick Links on this page are available " & _ 
                          "to unregistered and registered users."
    End If
End Sub

Protected Sub SaveProfile_Click(ByVal sender As Object, _
          ByVal e As System.EventArgs) Handles SaveProfile.Click
         'Get Profile of current user
    If User.Identity.IsAuthenticated Then
        Profile.GetProfile(User.Identity.Name)

        'Populate some Profile properties
        Profile.FirstName = "My FirstName"
        Profile.LastName = "My LastName"
    End If
     
End Sub

History

  • 09-13-2006 - Updated the source and article information
  • 07-10-2006 - Updated some typos and errors

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
United States United States
.Net Developer

Comments and Discussions

 
QuestionAttention to the case Pin
Gluups26-Jul-13 14:32
Gluups26-Jul-13 14:32 
Generalstrange problem Pin
ozkan.pakdil11-Jul-09 6:34
ozkan.pakdil11-Jul-09 6:34 
GeneralRe: strange problem Pin
Gluups26-Jul-13 14:40
Gluups26-Jul-13 14:40 
GeneralRe: strange problem Pin
ozkan.pakdil27-Jul-13 3:55
ozkan.pakdil27-Jul-13 3:55 
GeneralProfiler Pin
lav naphade24-Apr-09 23:39
lav naphade24-Apr-09 23:39 
GeneralRe: Profiler Pin
Gluups26-Jul-13 14:42
Gluups26-Jul-13 14:42 
GeneralData not passing to PropertyStringsValue field Pin
maven4champ25-Mar-09 18:31
maven4champ25-Mar-09 18:31 
GeneralMySQL Membership, Role, Personalization and Profile providers Pin
AlexRiley14-Feb-09 8:32
AlexRiley14-Feb-09 8:32 
QuestionWhat I did... [modified] Pin
ZachMcBunklesnot22-Sep-08 12:23
ZachMcBunklesnot22-Sep-08 12:23 
AnswerRe: What I did... Pin
mathinayagam12330-Sep-08 22:36
mathinayagam12330-Sep-08 22:36 
GeneralMySql 5 Code [modified] Pin
pc.huff25-Apr-08 9:22
pc.huff25-Apr-08 9:22 
GeneralRe: MySql 5 Code Pin
Hawkmoth7-Aug-08 3:47
Hawkmoth7-Aug-08 3:47 
GeneralRe: MySql 5 Code [modified] Pin
Hawkmoth7-Aug-08 4:43
Hawkmoth7-Aug-08 4:43 
AnswerRe: MySql 5 Code Pin
drobole21-Sep-08 14:33
drobole21-Sep-08 14:33 
Generalbugs [modified] Pin
Perth_shan3-May-07 23:02
Perth_shan3-May-07 23:02 
GeneralRe: bugs Pin
Edacio12-Oct-07 6:23
Edacio12-Oct-07 6:23 
Generalmysql 5.0 Pin
darkducke21-Apr-07 6:15
darkducke21-Apr-07 6:15 
General404 on the mentioned download Pin
Øyvind Sean Kinsey2-Mar-07 2:05
Øyvind Sean Kinsey2-Mar-07 2:05 
GeneralRe: 404 on the mentioned download Pin
verocp8-Jun-07 2:13
verocp8-Jun-07 2:13 
GeneralRe: 404 on the mentioned download Pin
FordGT9025-Jul-07 23:49
FordGT9025-Jul-07 23:49 
GeneralRe: 404 on the mentioned download Pin
cabaret3-Sep-07 22:57
cabaret3-Sep-07 22:57 
Generalnull reference Pin
Danillo5514-Dec-06 16:07
Danillo5514-Dec-06 16:07 
QuestionSetPropertyValues getting called for reading values? Pin
ratty762-Nov-06 1:20
ratty762-Nov-06 1:20 
AnswerRe: SetPropertyValues getting called for reading values? Pin
Edacio9-Nov-06 6:22
Edacio9-Nov-06 6:22 
GeneralWeb Application Projects Pin
jawstress28-Oct-06 3:53
jawstress28-Oct-06 3:53 

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.