Documentation Home
Connectors and APIs Manual
Download this Manual
PDF (US Ltr) - 4.5Mb
PDF (A4) - 4.5Mb


Connectors and APIs Manual  /  ...  /  Tutorial: Connector/NET ASP.NET Profile Provider

4.6.2.2 Tutorial: Connector/NET ASP.NET Profile Provider

This tutorial shows you how to use the MySQL Profile Provider to store user profile information in a MySQL database. The tutorial uses MySQL Connector/NET 6.9.9, MySQL Server 5.7.21 and Microsoft Visual Studio 2017 Professional Edition.

Many modern websites allow the user to create a personal profile. This requires a significant amount of code, but ASP.NET reduces this considerable by including the functionality in its Profile classes. The Profile Provider provides an abstraction between these classes and a data source. The MySQL Profile Provider enables profile data to be stored in a MySQL database. This enables the profile properties to be written to a persistent store, and be retrieved when required. The Profile Provider also enables profile data to be managed effectively, for example it enables profiles that have not been accessed since a specific date to be deleted.

The following steps show you how you can select the MySQL Profile Provider:

  1. Create a new ASP.NET web project.

  2. Select the MySQL Application Configuration tool.

  3. In the MySQL Application Configuration tool navigate through the tool to the Profiles page.

  4. Select the Use MySQL to manage my profiles check box.

  5. Select the Autogenerate Schema check box.

  6. Click Edit and then configure a connection string for the database that will be used to store user profile information.

  7. Navigate to the last page of the tool and click Finish to save your changes and exit the tool.

At this point you are now ready to start using the MySQL Profile Provider. With the following steps you can carry out a preliminary test of your installation.

  1. Open your web.config file.

  2. Add a simple profile such as the following example.

    <system.web>
      <anonymousIdentification enabled="true"/>
      <profile defaultProvider="MySQLProfileProvider">
        ...
        <properties>
          <add name="Name" allowAnonymous="true"/>
          <add name="Age" allowAnonymous="true" type="System.UInt16"/>
          <group name="UI">
            <add name="Color" allowAnonymous="true" defaultValue="Blue"/>
            <add name="Style" allowAnonymous="true" defaultValue="Plain"/>
          </group>
        </properties>
      </profile>
      ...

    Setting anonymousIdentification to true enables unauthenticated users to use profiles. They are identified by a GUID in a cookie rather than by a user name.

Now that the simple profile has been defined in web.config, the next step is to write some code to test the profile.

  1. In Design View, design a simple page with the added controls. The following figure shows the Default.aspx tab open with various text box, list, and button controls.

    Figure 4.2 Simple Profile Application

    Content is described in the surrounding text.

    These will allow the user to enter some profile information. The user can also use the buttons to save their profile, clear the page, and restore their profile data.

  2. In the Code View add the following code snippet.

    ...
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox1.Text = Profile.Name;
            TextBox2.Text = Profile.Age.ToString();
            Label1.Text = Profile.UI.Color;
        }
    }
      
    // Store Profile
    protected void Button1_Click(object sender, EventArgs e)
    {
        Profile.Name = TextBox1.Text;
        Profile.Age = UInt16.Parse(TextBox2.Text);
    }
      
    // Clear Form
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "";
    }
    
    // Retrieve Profile
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = Profile.Name;
        TextBox2.Text = Profile.Age.ToString();
        Label1.Text = Profile.UI.Color;
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Profile.UI.Color = DropDownList1.SelectedValue;
    }
    ...
  3. Save all files and build the solution to check that no errors have been introduced.

  4. Run the application.

  5. Enter your name, age, and select a color from the list. Now store this information in your profile by clicking Store Profile.

    Not selecting a color from the list uses the default color, Blue, that was specified in the web.config file.

  6. Click Clear Form to clear text from the text boxes and the label that displays your chosen color.

  7. Now click Retrieve Profile to restore your profile data from the MySQL database.

  8. Now exit the browser to terminate the application.

  9. Run the application again, which also restores your profile information from the MySQL database.

In this tutorial you have seen how to using the MySQL Profile Provider with Connector/NET.