Friday 8 January 2016

Customize User's Profile in ASP.NET Identity System with EF Database First approach

Introduction
The new ASP.NET Identity system is the preferred way to handle user authentication in ASP.NET applications, whether based on Web Forms or MVC. 
If you want to user database first approach with ASP.NET identity, you need to create two projects - One for getting the ASP.NET Identity tables and second, for integrating with your existing database and add User Profile data. 
Using this method, you can add User Profile data in a separate UserProfile  table (here we have taken Users table) along with AspNetUsers table, while registering a new user.

Getting the Asp.Identity Tables
  • Create an MVC Project with Authentication Individual User Account
  • Create a new test database in MS SQL Server. Open Sql Server → connect to server → Right Click on Databases folder → New Database → Database Name: TestDatabase → click ok.
  • In  Web.Config file change connectionstring named DefaultConnection and change initial catalog as your test database.
  • Run project and register a new user. This will create new tables in your test database.
  • Five Tables will be created AspNetRoles, AspNetUserClaims, AspNetUserLogins, AspNetUserRoles, AspNetUsers
  • Open test database and create scripts of all the tables. To create scripts in SQL server expand databases folder → Expand your TestDatabase  → Expand Tables → Right click on a table → Script Table as → CREATE To → File → Select location where you want to save files → give File name: → Click on save. Do it for all the five tables.
Integrating with your existing DataBase
  • Insert the scripted tables into existing database in SQL Server Management Studio.
  • Run these scripts in this order – AspNetRoles, AspNetUsers, AspNetUserRoles, AspNetUserLogins, AspNetUserClaims - in your existing project database (for ex. XYZDatabase)
  • Create relationship (Primary Key-Foreign Key) with your user table.
  • Relationship among tables is shown in the following diagram.

Add User Profile information in your database while registering a new user
  • Create new Web Project → MVC .
  • Create an EDMX file. To create, Right Click on the folder where you want to keep your edmx file → Add → New Item → In left panel click on Data → Select ADO.NET Entity Data Model → Give Name → Add → EF Designer from Database → Next → New Connection → Provide Server Name, Credentials for Authentication and select database name → Test Connection → Ok → It will create a connection string automatically  → Select “Yes, include sensitive data in the connection string” → Check “Save connection settings in App.Config as” → Set Name → Next → Select table to want to add → Finish.
  • It will create an edmx file. When you expand it, you will see your class files under modelname.tt.
  • In my project, edmx file is  in a separate data layer. Hence, It will create a connection string in App.Config file (later on you have to move the connection string to web.config file.). If your edmx file is in the same project, connection string will be created in web.config file. The connection string will be like this:
  • Now in  web.config file there should be two connection strings – one with provider name “System.Data.SqlClient” and another with provider name “System.Data.EntityClient” as shown below: 
  • In IdentityModels.cs file under Models, Change class name ApplicationUser to UserProfile  and change the ApplicationDbContext like this:

  • In AspNetUserLogin.cs file (under MyModel.edmx → MyModel.tt) add Key attribute as shown below:

  • In AccountController Register() method add User Profile data by creating an object “user” of UserProfile  and pass “user” in CreateAsync method.
  • Replace all the instances of  "ApplicationUser" to "UserProfile".

  • Now run your application and register a new user. You will see a newly registered user’s data will be stored in two tables AspNetUsers and Users.