为AspNetUser添加自定义字段

时间:2019-06-06 08:51:37

标签: asp.net-core asp.net-mvc-scaffolding ef-core-2.2

我是ASP.NET Core的新手。 如何在代码和UI中将自定义字段添加到用户模型中?

当前的用户管理用户界面是这样的,但是我想在模型,数据库和用户界面中添加一些额外的字段,例如信用或分数:

enter image description here

这是我的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; }
    }

1 个答案:

答案 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}中自定义数据}函数。