ASP.NET Core 8 Web API 端点不包含我的自定义字段

问题描述 投票:0回答:1

我创建了一个实体

ApplicationUser
并成功将其扩展到
IdentityUser
,并且迁移在数据库中创建了它。

但是当我转到注册端点时,它没有显示要注册的自定义字段:

实体

ApplicationUser

public class ApplicationUser : IdentityUser
{
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public int FavoriteTeam { get; set; }
    public string? PhotoPath { get; set; }
    public string? FirstLast { get; set; }
}

DbContext

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

节目

builder.Services.AddIdentityApiEndpoints<ApplicationUser>()
    .AddEntityFrameworkStores<ContextBase>();
     
-----

app.MapIdentityApi<ApplicationUser>();
asp.net-core-webapi identity .net-8.0 custom-properties
1个回答
0
投票

如果处理注册的 API 端点未配置为接受您的

ApplicationUser
的附加字段,则可能是有原因的。你可以试试下面的代码吗:

public class RegisterDto
{
    public string Email { get; set; }
    public string Password { get; set; }

    // Custom fields
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int FavoriteTeam { get; set; }
    public string PhotoPath { get; set; }
}

更新注册端点

public async Task<IActionResult> Register(RegisterDto model)
{
    var user = new ApplicationUser
    {
        UserName = model.Email,
        Email = model.Email,
        FirstName = model.FirstName,
        LastName = model.LastName,
        FavoriteTeam = model.FavoriteTeam,
        PhotoPath = model.PhotoPath
    };

    // other registration process
}
© www.soinside.com 2019 - 2024. All rights reserved.