Scaffold Identity Pages with ASP.NET Core | Dear Coder
Tags: csharp, security, tutorial, dotnet
Published: 2021-05-26 13:42:42

Dear Coder,


Let’s talk about the person who matters the most in our field.

No, it’s not you (though you’re enduringly important to me, dear Coder).

I’m talking about the user.

## How Do I Represent the User in Code?
I want the user to have a good user experience, so I build a model that inherits from AspNetCore’s IdentityUser. By doing this, I inherit a set of robust properties without having to write too much new code.

## Create User Model and Inherit Properties from IdentityUser

Let’s start with the User model:
![ToonUser Model](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvovv2pw910b9n7v7bi3.png)

In my ToonSpace project, I create a model named “ToonUser” and add some custom properties that IdentityUser doesn’t already cover, including:

```
FirstName
LastName
DisplayName
FullName
ImageData
ContentType
```

I also inherit the properties of IdentityUser by using the Microsoft.AspNetCore.Identity namespace and inheriting from Identity user when I name the class:
![Inherit from IdentityUser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/buwlrm1pgwsugbdoyo1p.png)

What properties does IdentityUser grant me access to? A lot.
![Properties inherited from IdentityUser](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pfhnhhfqeyxl6awecw4y.png)

With these properties appended to my ToonUser, I have the tools I need to allow users to register accounts and interact with my application.

## Modify Startup.cs and ApplicationDbContext

In my data folder, I access my ApplicationDbContext file and inherit IdentityDbContext of type ToonUser.
![Update ApplicationDbContext](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7q54si3z3em5pc7l8yo.png)

Then I add the ToonUser to my Identity service, along with Default UI and DefaultTokenProviders.
![update Startup.cs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qh8qam2aiav745v9ezo8.png)

## Scaffold Identity Pages
Now I’m ready to scaffold Identity pages for user accounts. I add a Scaffolded Item in my Areas > Identity folder and select “Identity.” 
![Scaffold Identity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hvbmucv0cvvglku5r626.png)

I override all files and select ApplicationDbContext as my data context class. Once I add the scaffolded Identity pages, my Identity folder will be filled with boilerplate razor pages.
![Scaffolded Identity Pages](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhog792n3cc2gc0xa5ru.png)

I access the code behind for the “register” view and add properties for First Name and Last Name because those properties are not the default with Identity pages. I also add fields to the View for the user to enter this information.
![Modify Code Behind](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6iyrltle27vu5jbdokd1.png)

Now that I have registration pages scaffolded and augmented to fit my ToonUser model, users can register an account on my application.


I hope these letters find you well, dearest Coder.

Until next time, godspeed in your keystrokes.


Clickity Clacks,



Kasey