DEV Community

Cover image for Scaffold Identity Pages with ASP.NET Core | Dear Coder
Kasey Wahl
Kasey Wahl

Posted on

8 2

Scaffold Identity Pages with ASP.NET Core | Dear Coder

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

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


Enter fullscreen mode Exit fullscreen mode

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

What properties does IdentityUser grant me access to? A lot.
Properties inherited from IdentityUser

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

Then I add the ToonUser to my Identity service, along with Default UI and DefaultTokenProviders.
update Startup.cs

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

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

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

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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (1)

Collapse
 
marissab profile image
Marissa B

Just found this in 2023 and it was exactly what I needed. Thank you! Being unable to create a user class using the default ApplicationDbContext during the scaffolding process was throwing me and I wanted to avoid having a totally separate user db/context.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️