1

I am a newbie in ASP.NET Core. How can I add custom fields to my user model both in code and UI?

Current user management UI is something like this, but I want to add some extra fields, like credit or scores, in model, DB & UI:

enter image description here

And this is my AspNetUser model :

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; }
    }
AVEbrahimi
  • 17,993
  • 23
  • 107
  • 210
  • 1
    I think this article covers exactly what you're looking for: https://medium.com/@scottkuhl/extending-asp-net-core-2-2-identity-management-c3cc657cc448 – hujtomi Jun 06 '19 at 13:20

1 Answers1

4

Custom user data is supported by inheriting from IdentityUser. It's customary to name this type ApplicationUser:

public class ApplicationUser : IdentityUser
{
    public string CustomTag { get; set; }
}

The detail steps to customize could be found in Customize the model document .

In ASP.NET Core 2.1 or later, Identity is provided as a Razor Class Library. For more information, see Scaffold Identity in ASP.NET Core projects .

After scaffolding , you can find the profile page in Areas.Identity.Pages.Account.Manage.Index.cshtml , you can add entities in InputModel of Index.cshtml.cs and customize the data in OnGetAsync and OnPostAsync function .

Nan Yu
  • 26,101
  • 9
  • 68
  • 148