Customizing User's Profile to add new fields in brand new database table
In
this post you will learn how to customize User’s Profile and add new
fields/properties/columns (FirstName, LastName and EmailID) in a brand new
table with ASP.NET Identity System.
In the image given below, look at right Sever Explorer, this is what we are going to achieve, a new table is there by name ‘UserProfileInfo’.
As
I mentioned earlier, ASP.NET Identity System uses Entity Framework Code First,
so we have opportunity to customize User’s Profile in a way we want. So, I’m
going one step further to generate a new database table to store User’s Profile
information.
Follow
the steps from here.
Step 1: Add new
properties in new class ‘UserProfileInfo’
Recall
last post, instead of adding new properties in ApplicationUser class, we will
create another class UserProfileInfo and place a virtual class field in ApplicationUser
class. Also, add a new DbSet in ApplicationDbContext. In the image given below,
we can see both formats. For the sake of demo, I will be adding UserProfileInfo in DbContext page but you should keep it separate.
Step 2: Update
Account Controller's Register action (Post Version)
In
above image, we moved FirstName, LastName and EmailID from ApplicationUser
class to a new class, so we need to update Register action also, look at the
following code.
Step 3: Add New Migration
Step 3: Add New Migration
Once
we done with above steps, bring the Package Manager Console and execute
following command.
Add-Migration "newtable"
This
command will generate a database script file, now execute following command to
run this script file against database.
Update-Database
Now,
all the new properties will turn into new database table.
Step 4: Run the
Application
Let's
go and run the application and try to create new user account. We will see a
new database table by name ‘UserProfileInfos’ up and it has user profile
information FirstName, LastName and EmailID.
Step 5: Accessing
User Profile Data
If
user is logged in, we can access user's profile information that we just added
following way in the controller.
Or
if we want to access these information directly on the view page, here is
another way.
@using Microsoft.AspNet.Identity;
@using Microsoft.AspNet.Identity.EntityFramework;
@using ProfileManagement.Models;
<div>
@{
if (Request.IsAuthenticated)
{
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var currentUser =
manager.FindById(User.Identity.GetUserId());
<p>@currentUser.UserProfileInfo.EmailID</p>
<p>@currentUser.UserProfileInfo.FirstName
@currentUser.UserProfileInfo.LastName</p>
}
}
</div>
Please
note I have used all the required namespaces.
In
next post, we will explore how to seed these database tables (AspNetUsers,
UserProfileInfos) that we just created here, we will seed all the information
including UserName, Password, FirstName, LastName, EmailID etc. This is also
going to be very interesting and informative.
Hope
this helps.
Thanks.
For those who trying to achieve this in Web Forms.
ReplyDeleteAs you know MVC as well as Web Forms uses same 'ASP.NET Identity System' architecture. In this article I talked about MVC way, if you want to achieve this in Web Forms, this comment is for you.
Open IdentityModels.cs class, inside that there will be a class ApplicationUser inherits IdentityUser. Here you can add your additional fields/properties. Same set of controls you need to place on Register.aspx form. Now inside Register.aspx.cs file, update var user = new ApplicationUser() { UserName = UserName.Text }; exactly the way i suggested in the article. Do not forget to update the database to match it.
I don't have sample code to show you now but things are always same in MVC and Web Forms as it uses same Identity model... if you try. Hope this helps.
If you still face any issue join our FB group https://www.facebook.com/groups/learnmvc/ and ask the questions with detailed description if possible screenshots.
Hello,
ReplyDeletei am saif. i want to add a simple log in where any student or any faculty can log in and view their personal data .Can u help me?
Thanks for the well written series.. Can you fix the invisible pictures in this article?
ReplyDeleteVery nice series of posts.. Bu won't be complete without including a section on how to update Profile Data of the user..
ReplyDeleteHi and thank you for your article. In your UserProfileInfo class you have an INT Id that looks just like a PK column for the UserProfileInfo table - then you populate the info before calling the createAsync() method - this might be a silly question (I just started learning entity) but how does the database and code know that the two are related if the User's Id(GUID) is not a foreign key in the UserProfileInfo table? How does it know the new record belongs to the new user, and how is the correct record pulled to populate that data when the user logs in?
ReplyDeleteHi,.. Awesome post... here how can i use my own database. please suggest me on this...
ReplyDeleteHow to update user profile by individual user
ReplyDeleteHi, do you have your code on GitHub or anywhere to look at? I'm getting an error that a principle isn't being declared.
ReplyDeleteHow can i custom Role table like this?, i want to add a field Permission to link to Permission table. I want to create a dynamic roles (not hard code)
ReplyDeleteThank u for this tutorial. I tried ur code but i am getting object null reference of 'userprofile'. I am using asp.net ideneity 2.2.1. All the relation are formed in db. I am bit new entity framework code first.
ReplyDeleteGood day, which method is OK, is it by create another table or by add field to exiting user profile table
ReplyDeletecreate another table approach is better
DeleteWorked perfectly for me (running VS Community 2017 RC and most recent versions of NuGet packages by now - 22-dec-2016). Thanks for sharing!
ReplyDelete