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


4.6.2.4 Tutorial: Simple Membership Web Provider

This section documents the ability to use a simple membership provider on MVC 4 templates. The configuration OAuth compatible for the application to login using external credentials from third-party providers like Google, Facebook, Twitter, or others.

This tutorial creates an application using a simple membership provider and then adds third-party (Google) OAuth authentication support.

Note

This feature was added in MySQL Connector/NET 6.9.0.

Requirements
  • Connector/NET 6.9.x or later

  • .NET Framework 4.0 or later

  • Visual Studio 2012 or later

  • MVC 4

Creating and Configuring a New Project

To get started with a new project, do the following:

  1. Open Visual Studio, create a new project of ASP.NET MVC 4 Web Application type, and configure the project to use .NET Framework 4.5. The following figure shows and example of the New Project window with the items selected.

    Figure 4.6 Simple Membership: New Project

    Content is described in the surrounding text.

  2. Choose the template and view engine that you like. This tutorial uses the Internet Application Template with the Razor view engine (see the next figure). Optionally, you can add a unit test project by selecting Create a unit test project.

    Figure 4.7 Simple Membership: Choose Template and Engine

    Content is described in the surrounding text.

  3. Add references to the MySql.Data, MySql.Data.Entities, and MySql.Web assemblies. The assemblies chosen must match the .NET Framework and Entity Framework versions added to the project by the template.

  4. Add a valid MySQL connection string to the web.config file, similar to the following example.

    <add
      name="MyConnection"
      connectionString="server=localhost;
                        UserId=root;
                        password=pass;
                        database=MySqlSimpleMembership;
                        logging=true;port=3305"
      providerName="MySql.Data.MySqlClient"/>
  5. Under the <system.data> node, add configuration information similar to the following example.

    <membership defaultProvider="MySqlSimpleMembershipProvider">
    <providers>
    <clear/>
    <add
      name="MySqlSimpleMembershipProvider"
      type="MySql.Web.Security.MySqlSimpleMembershipProvider,MySql.Web,
            Version=6.9.2.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"
      applicationName="MySqlSimpleMembershipTest"
      description="MySQLdefaultapplication"
      connectionStringName="MyConnection"
      userTableName="MyUserTable"
      userIdColumn="MyUserIdColumn"
      userNameColumn="MyUserNameColumn"
      autoGenerateTables="True"/>
    </providers>
    </membership>
  6. Update the configuration with valid values for the following properties: connectionStringName, userTableName, userIdColumn, userNameColumn, and autoGenerateTables.

    • userTableName: Name of the table to store the user information. This table is independent from the schema generated by the provider, and it can be changed in the future.

    • userId: Name of the column that stores the ID for the records in the userTableName.

    • userName: Name of the column that stores the name/user for the records in the userTableName.

    • connectionStringName: This property must match a connection string defined in web.config file.

    • autoGenerateTables: This must be set to false if the table to handle the credentials already exists.

  7. Update your DBContext class with the connection string name configured.

  8. Open the InitializeSimpleMembershipAttribute.cs file from the Filters/ folder and locate the SimpleMembershipInitializer class. Then find the WebSecurity.InitializeDatabaseConnection method call and update the parameters with the configuration for connectionStringName, userTableName, userIdColumn, and userNameColumn.

  9. If the database configured in the connection string does not exist, then create it.

  10. After running the web application, the generated home page is displayed on success (see the figure that follows).

    Figure 4.8 Simple Membership: Generated Home Page

    Content is described in the surrounding text.

  11. If the application executed with success, then the generated schema will be similar to the following figure showing an object browser open to the tables.

    Figure 4.9 Simple Membership: Generated Schema and Tables

    Content is described in the surrounding text.

  12. To create a user login, click Register on the generated web page. Type the user name and password, and then execute the registration form. This action redirects you to the home page with the newly created user logged in.

    The data for the newly created user can be located in the UserProfile and Webpages_Membership tables.

Adding OAuth Authentication to a Project

OAuth is another authentication option for websites that use the simple membership provider. A user can be validated using an external account like Facebook, Twitter, Google, and others.

Use the following steps to enable authentication using a Google account in the application:

  1. Locate the AuthConfig.cs file in the App_Start folder.

  2. As this tutorial uses Google, find the RegisterAuth method and uncomment the last line where it calls OauthWebSecurity.RegisterGoogleClient.

  3. Run the application. When the application is running, click Log in to open the log in page. Then, click Google under Use another service to log in (shown in the figure that follows).

    Figure 4.10 Simple Membership with OAuth: Google Service

    Content is described in the surrounding text.

  4. This action redirects to the Google login page (at google.com), and requests you to sign in with your Google account information.

  5. After submitting the correct credentials, a message requests permission for your application to access the user's information. Read the description and then click Accept to allow the quoted actions, and to redirect back to the login page of the application.

  6. The application now can register the account. The User name field will be filled in with the appropriate information (in this case, the email address that is associated with the Google account). Click Register to register the user with your application.

    Now the new user is logged into the application from an external source using OAuth. Information about the new user is stored in the UserProfile and Webpages_OauthMembership tables.

To use another external option to authenticate users, you must enable the client in the same class where we enabled the Google provider in this tutorial. Typically, providers require you to register your application before allowing OAuth authentication, and once registered they typically provide a token/key and an ID that must be used when registering the provider in the application.