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
/Oracle
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:
- Install Connector/Net 6.9, or download the No Install version.
- Net Framework 4.0 or greater.
- MVC 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.
- 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”.
- 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.
- 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.
- 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).
- 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"/> - 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> - 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.
- 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.
- 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.
- 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.
- If the database that you configure in your connection string doesn't exists, you need to create it empty.
- Now we're ready to run our web application, press F5 or the Run button in the tool bar.
- You'll see the following screen:
- If you go to your database used by the application you'll see some tables created, now we are using SimpleMembership.
- 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.
- 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.
- Go to the class AuthConfig.cs in the folder App_Start.
- In the method “RegisterAuth” uncomment the last line where is the call to the method “OauthWebSecurity.RegisterGoogleClient”.
- 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”.
- You will be asked for Google credentials.
- 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”.
- Now a page to register your user will be shown, click on “Register”.
- Now your new user is logged in in your application.
- 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