我创建了一个实体
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>();
如果处理注册的 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
}