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
.
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:
Step 3: Modify the Web.config.
<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.
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
If User.Identity.IsAuthenticated Then
Profile.GetProfile(User.Identity.Name)
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