如何将身份和用户管理添加到 aspnetcore web api?
How add identity and user management into aspnetcore web api?
我正在构建 asp.net 核心网站 api 有 3 种类型的用户:管理员、客户和程序员。这些角色中的每一个都有自己的工单列表。这些是我已经添加的 类 实体:
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 并将它们与他们的票联系起来, i.e.do 我只在 api 中留下门票 table?)或者像我已经做的那样让用户成为 api 的一部分(如果是这样,最好的改变方式是什么他们变成身份用户,以便我以后可以查询他们的票?)。有人有任何 tips/links/similar 代码示例吗?我将不胜感激:)
Identity 开箱即用地支持一组用户配置文件属性。最简单的解决方案是创建一个 AppUser
class 继承自 IdentityUser 并具有跨用户类型的所有属性。还为您的每种用户类型引入一个接口,以及它们支持的属性。
class AppUser: IdentityUser, IAdmin, IProgrammmer, IClient {...}
根据与用户关联的 IdentityRole,您可以将 AppUser 实例转换为适当的接口以访问特定于用户的属性。
我正在构建 asp.net 核心网站 api 有 3 种类型的用户:管理员、客户和程序员。这些角色中的每一个都有自己的工单列表。这些是我已经添加的 类 实体:
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 并将它们与他们的票联系起来, i.e.do 我只在 api 中留下门票 table?)或者像我已经做的那样让用户成为 api 的一部分(如果是这样,最好的改变方式是什么他们变成身份用户,以便我以后可以查询他们的票?)。有人有任何 tips/links/similar 代码示例吗?我将不胜感激:)
Identity 开箱即用地支持一组用户配置文件属性。最简单的解决方案是创建一个 AppUser
class 继承自 IdentityUser 并具有跨用户类型的所有属性。还为您的每种用户类型引入一个接口,以及它们支持的属性。
class AppUser: IdentityUser, IAdmin, IProgrammmer, IClient {...}
根据与用户关联的 IdentityRole,您可以将 AppUser 实例转换为适当的接口以访问特定于用户的属性。