Search code examples
c#asp.net-core-webapiasp.net-core-identityuser-management

How add identity and user management into aspnetcore web api?


I'm building asp.net core web api with 3 types of users: Admin, Clients and Programmers. Each of those roles have their own list of tickets. These are entity classes I've already added:

 public class User

    {
        [Key]
        public Guid Id { get; set; }
        [Required]
        [MaxLength(50)]
        public string FirstName { get; set; }
        [Required]
        [MaxLength(50)]
        public string LastName { get; set; }
        [Required]        
        public string Address { get; set; }

        [Required]       
        public string Zip { get; set; }
        [Required]
        [Phone]
        public string PhoneNumber { get; set; }
        [Required]
        [EmailAddress]
        public string EmailAddress { get; set; }
        [Required]
        public Gender Gender { get; set; }
        [Required]
        [MaxLength(50)]
        public string UserName { get; set; }
        [Required]
        [MaxLength(50)]
        public string Password { get; set; }
        [Required]
        public Role Role { get; set; }

        public virtual Programmer Programmer { get; set; }

        public virtual Admin Admin { get; set; }

        public virtual Client Client { get; set; }
    }


public class Client
    {

        public Guid clientId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual ICollection<Ticket> Tickets { get; set; } = new List<Ticket>();     
        public Guid Id { get; set; }
        public virtual User User { get; set; }
    }

 public class Admin
    {

        public Guid adminId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public virtual ICollection<Ticket> Tickets { get; set; } = new List<Ticket>();
        public Guid Id { get; set; }
        public virtual User User { get; set; }
    }

 public class Programmer
    {

        public Guid programmerId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public ProgrammingLanguage ProgrammingLanguage { get; set; }
        public Experience Experience { get; set; }
        public DetailOriented DetailOriented { get; set; }
        public FieldOfInterest FieldOfInterest { get; set; }


        public virtual ICollection<Ticket> Tickets { get; set; } = new List<Ticket>();

        public Guid Id { get; set; }
        public virtual User User { get; set; }

    }

I've already created regular DbContext and implemented functionality of my api using that DbContext. Now I want to change users to be IdentityUsers. I'm not sure should I make a new project where UserIdentity will be handled and then pass those users to my api(if so, how can I pass them to api and connect them with their tickets, i.e.do I leave only tickets table in api?) or make users as part of an api as I already did(if so, what would be the best way to change them into identity users so that I can query their tickets later?). Does anyone have any tips/links/similar code samples or something? I would be grateful :)


Solution

  • Out of the box, Identity supports a single set of user profile properties. The easiest solution would be to create an AppUser class that inherits from IdentityUser with ALL of the properties across your user types. Also introduce an interface for each of your user types with the properties they support.

    class AppUser: IdentityUser, IAdmin, IProgrammmer, IClient {...}
    

    Based on the IdentityRole associated with the user, you can cast the AppUser instance to the appropriate interface to access the user-specific properties.