Application authentication
.NET Core ASP.NET Core ASP.NET Core Identity C# Entity Framework Core OAuth Razor Visual Studio Web API

How to Customize User Identity in .NET Core Applications

Welcome to today’s post.

In a previous post I showed how to scaffold ASP.NET Identity functionality into an ASP.NET Core Web Application using Visual Studio 2017.

In today’s post I will extend this by showing you how to add additional fields to customize your user identity store for .NET Core applications and how to upgrade existing models, controllers and views to display and edit the new fields in the application.

The default user identity that is generated for you using the build-in Identity scaffold project in Visual Studio 2017 has some basic fields.

These are:

UserName

Email

ContactNumber

PhoneNumber

NormalizedEmail

NormalizedUserName

SecurityStampPasswordHash

f we wish to include additional fields for first name, last name, and date of birth we can add these fields into the ApplicationUser model as shown:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;

namespace BookLoan.Models
{

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
    }
}

Note: The [PersonalData] data attribute is available from .NET Core 2.1 onwards.

Next, apply the migration to the database.

Open the PowerShell console.

Run the migration command:

Add-Migration add_useridentity_newfields_09_12_2019

Next, run:

Update-Migration

Next, check the new fields have been added successfully by opening SSMS or the table through the Object Explorer.

Next, update the any dependent application models:

\Models\ManageUserViewModel\UserViewModel.cs,

\Models\ManageViewModel\IndexViewModel.cs,

\Models\AccountViewModels\RegisterViewModel.cs,

dependent controllers:

\Controller\ManageController.cs

\Controller\AccountController.cs

and views \Manage\Index.cshtml and \Account\Register.cshtml page to declare variables for and display these new edit fields.

With the model changes, below is an example of the first model change:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BookLoan.Models.ManageUserViewModels
{
    public class UserViewModel
    {
        public string ID { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
	    // custom personal fields
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
    }
}

The remining models can be updated similarly with the custom fields.

For controller changes for Index.cs, HTTP GET and POST, below is the change for HTTP GET:

[HttpGet]
public async Task<IActionResult> Index()
{
   var user = await _userManager.GetUserAsync(User);
   if (user == null)
   {
      throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
   }
   var model = new IndexViewModel
   {
      Username = user.UserName,
      Email = user.Email,
      PhoneNumber = user.PhoneNumber,
      FirstName = user.FirstName,
      LastName = user.LastName,
      DOB = user.DOB,		  
      IsEmailConfirmed = user.EmailConfirmed,
      StatusMessage = StatusMessage
   };

   return View(model);
}

In your POST controller method, a call to update the user data using

await _userManager.UpdateAsync(user);

is required.

For changes to AccountController.cs, changes to the Register() get and post methods are required.

For changes to the Index.cshtml profile update and Register.cshtml profile creation add the markup:

<div class="form-group">
   <label asp-for="FirstName"></label>
      <input asp-for="FirstName" class="form-control" />
</div>

<div class="form-group">
   <label asp-for="LastName"></label>
   <input asp-for="LastName" class="form-control" />
</div>

<div class="form-group">
   <label asp-for="DOB"></label>
   <input asp-for="DOB" class="form-control" />
</div>

After all these changes have been applied and the application built, then test the registration, update profile and login functions.

Now open SSMS and query the database for the newly created user and registration data:

/****** Script for SelectTopNRows command from SSMS  ******/
SELECT TOP (1000) [Id]
      ,[UserName]
      ,[DOB]
      ,[FirstName]
      ,[LastName]
FROM [AspNetUsers]

Test also the manage user profile and update a property from one of the custom fields:

Now check the data by query:

If the update is successful, then the new fields will be modified.

That’s all for today!

I hope you learnt something useful.

In a future post I will be applying our data to help us with claims-based authentication and authorizations.

Social media & sharing icons powered by UltimatelySocial