You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AspNetCore Identity中为ApplicationUser添加存储于AspNetUsers表的属性

How to Add Custom Properties to ApplicationUser in ASP.NET Core Identity

Hey there! Adding custom properties to your ApplicationUser (stored in the AspNetUsers table) in ASP.NET Core Identity is a common and straightforward task. Let me break down the exact steps you need to follow:

Step 1: Modify the ApplicationUser Class

First, locate your ApplicationUser.cs file (usually in the Models or Identity folder of your project). This class inherits from IdentityUser by default—simply add your custom properties here. For example:

public class ApplicationUser : IdentityUser
{
    // Custom properties to add to AspNetUsers table
    [Required]
    [Display(Name = "Full Name")]
    public string FullName { get; set; }

    public DateTime? DateOfBirth { get; set; } // Nullable for optional field

    public bool IsAccountActive { get; set; } = true; // Default value
}

Step 2: Update Your DbContext

Ensure your application's DbContext is configured to use your custom ApplicationUser instead of the default IdentityUser. If your DbContext inherits from IdentityDbContext, specify ApplicationUser as the generic parameter:

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

If you already had a DbContext set up, double-check that the generic argument is ApplicationUser (not the default IdentityUser).

Step 3: Create and Apply Database Migrations

Next, you need to update your database schema to include the new columns. Open the Package Manager Console (or your terminal) and run these commands:

# Create a new migration with a descriptive name
Add-Migration AddCustomUserProperties

# Apply the migration to your database
Update-Database

This will add the new columns (FullName, DateOfBirth, IsAccountActive) to your AspNetUsers table automatically.

Step 4: Update UI and ViewModels (If Needed)

If you want users to provide these custom properties during registration or profile editing, you'll need to update the relevant view models and views:

Update the ViewModel

For example, in RegisterViewModel.cs, add the custom properties:

public class RegisterViewModel
{
    // Existing properties (Email, Password, ConfirmPassword, etc.)

    [Required]
    [Display(Name = "Full Name")]
    public string FullName { get; set; }

    [Display(Name = "Date of Birth")]
    [DataType(DataType.Date)]
    public DateTime? DateOfBirth { get; set; }
}

Update the View

In Register.cshtml, add form fields for the new properties:

<div class="form-group">
    <label asp-for="Input.FullName"></label>
    <input asp-for="Input.FullName" class="form-control" />
    <span asp-validation-for="Input.FullName" class="text-danger"></span>
</div>

<div class="form-group">
    <label asp-for="Input.DateOfBirth"></label>
    <input asp-for="Input.DateOfBirth" class="form-control" />
    <span asp-validation-for="Input.DateOfBirth" class="text-danger"></span>
</div>

Update the Controller Action

In your AccountController's Register POST action, map the view model values to the ApplicationUser instance:

var user = new ApplicationUser 
{ 
    UserName = Input.Email, 
    Email = Input.Email,
    FullName = Input.FullName,
    DateOfBirth = Input.DateOfBirth
};

Step 5: Access the Custom Properties

Once everything is set up, you can access the custom properties anywhere you have access to the ApplicationUser—for example, via UserManager or HttpContext.User:

// Using UserManager
var currentUser = await _userManager.GetUserAsync(User);
var userFullName = currentUser.FullName;

// Or when fetching a user by ID
var user = await _userManager.FindByIdAsync(userId);
bool isActive = user.IsAccountActive;

Quick Notes

  • If a property is optional, use a nullable type (like DateTime?) so the corresponding database column allows NULL values.
  • You can add data annotations (like [Required], [MaxLength]) to enforce validation rules, just like any other model property.

内容的提问来源于stack exchange,提问作者Greg Gum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:13:26