Course contentsShow
← C#
Lesson 1184 of 1,90811. Identity, Security, and ClaimsFree lesson

Managing User Profiles and Metadata

Extend the IdentityUser model to support custom user-specific data and attributes.

In this lesson, you will learn how to extend the default IdentityUser model to store custom application-specific metadata, such as a user’s display name, birth date, or profile preferences.

Why Extend IdentityUser?

While the default IdentityUser provides essential fields like Email, PhoneNumber, and PasswordHash, real-world applications often require additional data points that don't fit into the standard schema. Instead of creating a separate table with a foreign key back to the user, you can extend the Identity framework directly. This keeps your user data organized within the same entity context and makes it easily accessible during authentication and authorization flows.

How to Implement Custom Attributes

To add custom metadata, you create a class that inherits from IdentityUser and add your desired properties.

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

After defining your class, you must update your DbContext to inherit from IdentityDbContext<ApplicationUser> instead of the standard IdentityDbContext. This tells Entity Framework Core that ApplicationUser is the new primary entity for your identity operations.

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) 
        : base(options) { }
}

Once the model is updated, you perform a migration as usual (Add-Migration AddCustomUserFields followed by Update-Database). EF Core will automatically update the AspNetUsers table by adding columns for FirstName, LastName, and the other properties you defined.

Managing the Metadata

Because your UserManager<ApplicationUser> now understands the ApplicationUser type, you can easily access these fields throughout your application. For example, when a user logs in, you can access user.FirstName directly from the UserManager or the ClaimsPrincipal if you have mapped these properties to user claims. This approach ensures your user profile logic remains type-safe and performant, avoiding unnecessary joins when fetching core profile information.

Key Takeaway: By inheriting from IdentityUser and updating your DbContext, you can seamlessly integrate custom business data directly into your identity schema, ensuring a clean and efficient data access pattern for user profiles.