How To: Using SimpleMembserhipProvider with MySql Connector/Net.

Posted by Francisco Tirado on Oracle Blogs See other posts from Oracle Blogs or by Francisco Tirado
Published on Fri, 15 Aug 2014 15:14:00 +0000 Indexed on 2014/08/18 16:37 UTC
Read the original article Hit count: 299

Filed under:

Now on Connector/Net 6.9 the users will have the ability to use SimpleMembership Provider on MVC4 templates. The configuration is very simple and also have compatibility with OAuth, in this post we'll explain step by step how to configure it in a MVC 4 Web Application.

Requirements 

The requirements to use SimpleMembership with Connector/Net are:

  1. Install Connector/Net 6.9, or download the No Install version.
  2. Net Framework 4.0 or greater.
  3. MVC 4 
  4. Visual Studio 2012 or newer version

Creating and configuring a new project

In this example we'll use VS2012 to create the project basis on the Internet Aplication template and using Entity Framework to manage the User model.

  1. Open VS 2012 and create a new project, we'll create a new MVC 4 Web Application and configure the project to use Net Framework 4.5. Type a name for the project and then click “Ok”.
    Project Creation
  2. In the next dialog we'll choose the “Internet Application” template and use Razor as engine without creating a test project. Click “Ok” to continue.
    Template Selection
  3. Now we have a new project with the templates necessaries to run a Web Application with the default values. We'll use the current files to continue working.
  4. If you have installed Connector/Net you can skip this step, if you don't have installed but you're planning to do it, please install it and continue with the next step. If you're using the No Install version of Connector/Net we'll need to add the references to our project, the assemblies needed are: MySql.Data, MySql.Data.Entities and MySql.Web. Be sure that the assemblies chosen match the Net Framework version used in our project and the MySql.Data.Entities is compatible with EF5 (EF5 is the default added by the project).
  5. Now open the “web.config” file, and under the <connectionStrings> node add a connection string that points to a MySql instance. We'll use the following connection configuration:
    <add name="MyConnection" connectionString="server=localhost;UserId=root;password=pass;database=MySqlSimpleMembership;" providerName="MySql.Data.MySqlClient"/>
  6. Under the node <system.web> we'll add the following configuration:
    <membership defaultProvider="MySqlSimpleMembershipProvider">
    <providers>
    <clear/>
    <add name="MySqlSimpleMembershipProvider" type="MySql.Web.Security.MySqlSimpleMembershipProvider,MySql.Web,Version=6.9.3.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" applicationName="MySqlSimpleMembershipTest" description="MySQLdefaultapplication" connectionStringName="MyConnection"  userTableName="UserProfile" userIdColumn="UserId" userNameColumn="UserName" autoGenerateTables="True"/>
    </providers>
    </membership>
  7. In the previous configuration the mandatory properties are: connectionStringName, userTableName, userIdColumn, userNameColumn and autoGenerateTables. If the other properties are not provided a default value is set to it but if the mandatory properties are not set a ProviderException will be thrown. The valid properties for the MySqlSimpleMembership are the same used for MySqlMembership plus the mandatory fields.
  8. UserTableName: Name of the table where will be stored the user, this table is independent from the schema generated by the provider and can be edited later by the user. UserId: name of the column that will store the id for the records in the userTableName. UserName : name of the column that will store the name/user for the records in the userTableName. The connectionStringName property must match a connection string defined in web.config file.
  9. Once the configuration is done in web.config, we need to be sure that our database context for the Users Table point to the right connection string. In our case we just need to update the class UsersContext in the file AcountModel.cs in the Models folder. The file also contains the UserProfile class which match the configuration for our UserTable.
  10. Other class that needs to be updated is the SimpleMembershipInitializer in the file InitializeSimpleMembershipAttribute.cs in the Filters folder. In that class we'll see a call to the method “WebSecurity.InitializeDatabaseConnection”, in that call is where we need to update the parameters to match our configuration.
  11. If the database that you configure in your connection string doesn't exists, you need to create it empty.
  12. Now we're ready to run our web application, press F5 or the Run button in the tool bar.
  13. You'll see the following screen:
    Index Page
  14. If you go to your database used by the application you'll see some tables created, now we are using SimpleMembership.
    DB Schema
  15. Now create a user, click on “Register” at the top-right in the web page. Type your user name and password, then click on “Register”. You'll be redirected to the home page and you'll see the name of your user at the top-right page.
    User Registered
  16. If you take a look on the tables just created in your database you will find the data about the user you just register. In our case the tables that contains the information are UserProfile and Webpages_Membership. 

Configuring OAuth

Other option to access your website will be using OAuth, so you can validate an user using an external account like Facebook, Twitter, Google, etc. In this post we'll enable the authentication for Google account in our application.

  1. Go to the class AuthConfig.cs in the folder App_Start.
  2. In the method “RegisterAuth” uncomment the last line where is the call to the method “OauthWebSecurity.RegisterGoogleClient”.
  3. Run the application. Once the application is running click on “Login”. You will see at the right side the option to login using a Google account, click on “Google”. 
    Login
  4. You will be asked for Google credentials.
    Google User Login
  5. If your login is successful you'll see a message asking for your approval to give permission to your site to access your information. Click on “Accept”.
    Localhost Permission
  6. Now a page to register your user will be shown, click on “Register”.
    Google User Registration
  7. Now your new user is logged in in your application.
    Google User Logged In
  8. You can take a look of the user information created in the tables  UserProfile and Webpages_OauthMembership.

If you want to use another external option to authenticate users you must enable the client in the same class where we enable the Google authentication, but for others providers is mandatory to register your Application in their site. Once you have register your application they will give you a token/key and the id for your application, that information you're going to use it to register the client.

Thanks for reading.

© Oracle Blogs or respective owner

Related posts about /Oracle