简体   繁体   English

NET Core 用户身份分组

[英]NET Core identity grouping of users

I currently have a project that uses identity and individual users have access to their own resources (sub database table linked by application user id).我目前有一个使用身份的项目,个人用户可以访问他们自己的资源(由应用程序用户 ID 链接的子数据库表)。 I'd like each user who signs up (admin) to then be able to invite new users to their 'group' and all share the same resource.我希望每个注册(管理员)的用户都能够邀请新用户加入他们的“组”,并且所有人都共享相同的资源。

I'm looking at implementing a 'Groups' and 'UserGroups' table similar to the current 'Roles' and 'UserRoles' tables.我正在考虑实施类似于当前“角色”和“用户角色”表的“组”和“用户组”表。 Does the following code look ok to achieve this?以下代码看起来可以实现这一目标吗?

  public class ApplicationUser : IdentityUser
  {
    [MaxLength(30)]
    public string FirstName { get; set; }
    [MaxLength(30)]
    public string LastName { get; set; }
    [MaxLength(100)]
    public string Company { get; set; }
    [MaxLength(30)]
    public string Telephone { get; set; }
    [MaxLength(15)]
    public string Postcode { get; set; }
    public DateTime Created { get; set; } = DateTime.UtcNow;
    public DateTime PasswordResetTime { get; set; } = DateTime.UtcNow;
    public bool PasswordReset { get; set; } = false;
    public virtual ICollection<GSMSite> GSMSites { get; set; }
  }

 public class UserGroups
 {
    public virtual ApplicationUser ApplicationUser { get; set; }
    public virtual Groups Groups { get; set; }
 }

 public class Groups
 {
    public Guid Id { get; set; }
    [MaxLength(30)]
    public string Name { get; set; }
 }

You are trying to achieve Many to Many relationship.您正在尝试实现多对多关系。 Firstly by convention You should name Classes with singular form:首先按照惯例你应该用单数形式命名类:

  • UserGroups -> UserGroup群组- >用户组
  • Groups -> Group->

You should also put ICollection<UserGroup> in both AplicationUser and Group:您还应该将ICollection<UserGroup>放在 AplicationUser 和 Group 中:

public virtual ICollection<UserGroup> UserGroups

And change UserGroup to:并更改用户组到:

 public class UserGroup
 {
    public Guid ApplicationUserId { get; set;}
    public virtual ApplicationUser ApplicationUser { get; set; }
    public Guid GroupId { get; set; }
    public virtual Groups Groups { get; set; }
 }

And then the resource You are talking about should be linked to Group not ApplicationUser .然后您正在谈论的资源应该链接到Group而不是ApplicationUser

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM