Custom ASP. NET identity (iii)-Implement a custom MySQL ASP. NET Identity storage provider

Source: Internet
Author: User
Tags mysql client mysql download oauth

label:

ASP.NET Identity is an extensible system. You can create your own storage provider and integrate it into your application without refactoring your application. This chapter will introduce how to create an ASP.NET Identity MySQL storage provider. For an overview of how to create a custom storage provider, please see Custom ASP.NET Identity (1)-Custom ASP.NET Identity Storage Provider. To complete this guide, you must install Visual Studio 2013 Update 2. This guide will include the following:
How to create a MySQL database instance on Azure.
How to use MySQL client tool (MySQL Workbench) to create tables and manage databases in Azure.
How to replace the default ASP.NET Identity storage implementation with a custom implementation in the MVC project.
Download the complete project
After completing this guide, you will have an MVC application project that uses the ASP.NET Identity of MySQL database residing on Azure.

 

You can download the complete MySQL storage provider code from AspNet.Identity.MySQL (CodePlex).

 

step
In this guide you will complete:

1. Create a MySQL database on Azure

2. Create an ASP.NET Identity table structure in MySQL

3. Create an MVC application and configure it to use the MySQL provider

4. Run the application

This article does not contain an introduction to the structure of ASP.NET Identity and how to implement a custom storage provider. For this information, please refer to Custom ASP.NET Identity (1)-Custom ASP.NET Identity Storage Provider.

 

Review the MySQL storage provider class
Going back to the previous steps to create a MySQL storage provider, let's look at the classes that make up the storage provider. You need these classes to manage database operations and applications call them to manage users and roles. Storage class

IdentityUser-Contains user attributes.
UserStore-Contains operations for adding, updating, or retrieving users.
IdentityRole-Contains role attributes.
RoleStore-contains operations to add, delete, update and retrieve roles.
 

Data access layer class In this example, the data access class contains SQL statements that manipulate data tables; however, in your code you may want to use an object-relational mapping (ORM) framework, such as Entity Framework or NHibernate. In particular, your application may experience performance problems without the lazy loading and object caching included in the ORM. For more information, please refer to ASP.Net Identity 2.0 without Entity Framework?
MySQLDatabase-Contains connections and methods for performing MySQL database operations. Both UserStore and RoleStore instantiate an instance of this class.
RoleTable-Contains data table operations for storing roles.
UserClaimsTable-Contains data table operations for storing user Claims.
UserLoginsTable-Contains data table operations that store user login information.
UserRoleTable-Contains data table operations that store user assigned roles.
UserTable-Contains data table operations for storing users.
 

Create a MySQL database instance on Azure
1. Log in to Azure Portal.

2. Click the + NEW button on the page, and select STORE.

3. In the Choose and Add-on wizard, select ClearDB MySQL Database and click Next on the right side of the dialog button

4. The default is Free plan, modify the Name to IdentityMySQLDatabase. After selecting the area, click Next.

 

5. Click on the checkbox to complete.

6. After the database is created, you can manage the database from the ADD-ONS tab of the management portal.

7. Click CONNECTION INFO at the bottom of this page to view the link information.

8. Click the Copy button to copy the link string and apply it to your MVC application.

 

Create an ASP.NET Identity table in the MySQL database Install MySQL Workbench tool to connect and manage MySQL database
Install the MySQL Workbench tool. The tool can be downloaded from the MySQL download page.
Run the application and click MySQLConnections + to add a new link. This guide can be easily created using the MySQL database connection string created from Azure.
After the connection is established, open a new Query tab; paste the commands in the MySQLIdentity.sql file into the query window, and execute it to finish creating the database table.
Now all the MySQL tables required by ASP.NET Identity have been created on Azure. As shown below.
 

Create MVC application from template and configure it to use MySQL provider
If necessary, install Visual Studio Express 2013 for Web or Visual Studio 2013 Update 2. Download the ASP.NET.Identity.MySQL project from CodePlex

Browse the URL of the resource library AspNet.Identity.MySQL (CodePlex).
Under the source code.
Unzip the .zip file to a local folder.
Open the AspNet.Identity.MySQL solution and generate it.
Create a new MVC application from the template
Right click on AspNet.Identity.MySQL solution and Add, New Project
In the Add New Project dialog, select Visual C #-> Web and then select ASP.NET Web Application. Enter the project name IdentityMySQLDemo; then click the OK button.
In the New ASP.NET Project dialog box, select the MVC template and keep the default options unchanged (including: Individual User Accounts as the authentication method) and click OK.

In the Solution Manager, right-click the IdentityMySQLDemo project and select Manage NuGet Packages. Enter Identity.EntityFramework in the Find text box. Select this package in the results list and click Uninstall. You will be prompted to uninstall the dependent package EntityFramework. Click Yes, we will no longer use this package in this application.

Right-click the IdentityMySQLDemo project, select Add, Reference, Solution, Projects; select AspNet.Identity.MySQL project and click OK.
In the IdentityMySQLDemo project, replace all references to
using Microsoft.AspNet.Identity.EntityFramework;
for
using AspNet.Identity.MySQL;
In IdentityModels.cs, set ApplicationDbContext to MySqlDatabase and include a constructor that accepts a connection name parameter.
public class ApplicationDbContext: MySQLDatabase
{
    public ApplicationDbContext (string connectionName)
        : base (connectionName)
    {
    }

    public static ApplicationDbContext Create ()
    {
        return new ApplicationDbContext ("DefaultConnection");
    }
}
Open the IdentityConfig.cs file. In the ApplicationUserManager.Create method, replace UserManager with the following code:
var manager = new ApplicationUserManager (
    new UserStore <ApplicationUser> (
    context.Get <ApplicationDbContext> () as MySQLDatabase));
Open the web.config file and replace the contents of the highlighted section of the DefaultConnection string with the information of the MySQL database created earlier:
<add name = "DefaultConnection" connectionString = "Database = IdentityMySQLDatabase;
Data Source = <DataSource>; User Id = <UserID>; Password = <Password> "
providerName = "MySql.Data.MySqlClient" />
Stealth application and connect to MySQL DB
Email click IdentityMySQLDemo project and Set as Startup Project.
Click Ctrl + F5 to create and run the application.
Click the Register tab at the top of the page.
After entering the user name and password, click Register.
A new user is registered and logged in.
Return to the MySQL Workbench tool and check the contents of the IdentityMySQLDatabase table. The new user you registered already exists in the table.
Next step
For how to enable other authentication methods in the application, please refer to Create an ASP.NET MVC 5 App with Facebook and Google OAuth2 and OpenID Sign-on.

To learn how to use OAuth in your database and set up users to restrict access to roles in your application, see Deploy a Secure ASP.NET MVC 5 app with Membership, OAuth, and SQL Database to Azure.

Custom ASP.NET Identity (3)-Implement custom MySQL ASP.NET Identity storage provider

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.