我是ASP.NET Core的新手。 如何在代码和UI中将自定义字段添加到用户模型中?
当前的用户管理用户界面是这样的,但是我想在模型,数据库和用户界面中添加一些额外的字段,例如信用或分数:
这是我的AspNetUser模型:
public partial class AspNetUsers
{
public AspNetUsers()
{
AspNetUserClaims = new HashSet<AspNetUserClaims>();
AspNetUserLogins = new HashSet<AspNetUserLogins>();
AspNetUserRoles = new HashSet<AspNetUserRoles>();
AspNetUserTokens = new HashSet<AspNetUserTokens>();
}
public string Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string ConcurrencyStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public double Credit { get; set; }
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
public virtual ICollection<AspNetUserTokens> AspNetUserTokens { get; set; }
}
答案 0 :(得分:2)
通过继承IdentityUser
支持自定义用户数据。通常将这种类型命名为ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string CustomTag { get; set; }
}
自定义的详细步骤可以在Customize the model文档中找到。
在ASP.NET Core 2.1或更高版本中,身份作为Razor类库提供。有关更多信息,请参见Scaffold Identity in ASP.NET Core projects。
搭建后,您可以在Areas.Identity.Pages.Account.Manage.Index.cshtml
中找到个人资料页面,可以在InputModel
的{{1}}中添加实体,并在Index.cshtml.cs
和{{1}中自定义数据}函数。