Extending ASP.NET Core 2.2 Identity Management

Microsoft gives you a really easy way to add users to your ASP.NET Core site. But there are two things I find myself changing right away: also asking for a user’s name and changing the user primary key from a string to an integer.

Let’s start by talking about why I want to make these changes.

First, I like to ask the user for either their first and last name, or a username. The default identity management templates rely heavily on email addresses only and not everyone is going to want to expose their email address to every other user on your site.

Second, I like to change the primary key from a string to an integer to match the rest of my data model. I tend to prefer integers over GUIDs, but I don’t know anyone that relies on strings.

I am assuming you already have a web application with user authentication enabled. You can set this up by selecting Individual User Accounts as the authentication type when creating your project. Or you can add it manually to an application by following these instructions.

Image for post
Image for post
Individual User Accou

Either way when you run your application you should have a working Login section.

For this example I will be using Razor Pages. MVC is very similar.

Add New Models

Let’s start by adding two new models to our application. In your Models folders create a new class called AppRole that extends IdentityRole. This can be used to assign special roles to users like “admin”.

And then add a AppUser class that extends IdentityUser so we can gather more data about them.

Both of these classes are marked with <int>. This tells Identity Management we want to use integer based primary keys.

AppUser marks its properties with some standard data annotations to make the first and last name required with a max length of 20 characters. These properties are also marked with PersonalData, “so it’s automatically available for download and deletion. Making the data able to be downloaded and deleted helps meet GDPR requirements.”

I also added a FullName property to consistently format a user’s name throughout the application.

Update the Database Context

The database context extends IdentityDbContext, we need to update it to tell it about our new classes and primary key strategy.

We also want to add or update or OnModelCreating to ignore the FullName property we added. It’s a computed property we don’t want to store in the database.

Update Startup

Our Startup class is currently referencing the default identity. We need to update it to our custom identity.



Update _LoginPartial.cshtml

The login control in the toolbar also has references that need to be updated. (Don’t forget to add a using statement to your models folder here.)

And we are no longer going to show the user their email address. Instead let’s show them their name.



Override Default Identity Pages

  1. Right click on the project name.
  2. Select Add > New Scaffolded Item.
  3. Select Identity on the left menu.
  4. Select Add.
  5. Choose Account/Manage/Index and Account/Register.
  6. Select your existing database context class.

This will create new pages for user registration and the user profile page where we can ask for the first and last name.


Copy the HTML from your current file located in the Pages/Shared folder to the new one in the Areas/Identity/Pages folder to make sure that are the same.


Add first and last name properties to the InputModel class.

Add these property values to the user variable declaration in OnPostAsync


Add the input fields for first and last name just below the asp-validation-summary control.


This is the class in the Areas/Identity/Pages/Account/Manage folder.

Add first and last name properties to the InputModel class again.

Update the Input variable in OnGetAsync to populate these properties.

Save the properties in the OnPostAsync near the end of the method.


Add the input fields for first and last name just below the email input and above the phone number.

Update Migrations

If you try to add a new migration it will fail because of the primary key change. The easy solution is to delete your migration folder and your database and then add a new migration.

Package Manager Console Example:

Add-Migration Initial -o Data\Migrations


Run your application and you should be able to register an account with a first and last name, see the user first name in the navigation bar and be able to update the first and last name.

You can now use the AppUser.Id property in your own models to reference the user record like you would any other model.

Seeding Test Data

If you are seeding test data you can do so in your extension class like this:

In your Startup class’s ConfigureServices method below service.AddIdentity add the following:

Here is my Main method in my Program class that kicks off database seeding. Your implementation of database seeding may be different.

You should now have an identity model that does not expose the user’s email address so easily and let’s you add integer based primary key references to your model.

You can download the complete working example here on GitHub.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store