如何将身份和用户管理添加到aspnetcore Web API中?

时间:2019-12-05 16:56:08

标签: c# asp.net-core-webapi asp.net-core-identity user-management

我正在使用3种类型的用户构建asp.net核心Web api:管理员,客户端和程序员。每个角色都有其自己的票证列表。这些是我已经添加的实体类:

 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; }

    }

我已经创建了常规的DbContext并使用该DbContext实现了api的功能。现在,我想将用户更改为IdentityUsers。我不确定是否应该新建一个将处理UserIdentity的项目,然后将这些用户传递给我的api(如果这样,我如何将他们传递给api并与他们的票证建立联系,即我只在其中保留票证表api?)还是像我已经做过的那样使用户成为api的一部分(如果是,将其更改为身份用户以便以后可以查询其票证的最佳方法是什么?)。是否有人有任何提示/链接/类似的代码示例或其他内容?我将不胜感激:)

1 个答案:

答案 0 :(得分:0)

开箱即用,Identity支持一组用户配置文件属性。最简单的解决方案是创建一个AppUser类,该类从IdentityUser继承您的用户类型中的所有属性。还要为每种用户类型引入一个界面,并为其提供支持的属性。

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

基于与用户关联的IdentityRole,可以将AppUser实例强制转换为适当的接口以访问用户特定的属性。