ASP.Net MVC应用程序与多个用户



我正在创建一个ASP。与设计、建筑等相关的asp.net MVC应用。我需要有两种类型的用户-普通人和公司。在这里,我读到了如何做到这一点。我创建了基本类ApplicationUserPersonUserCompanyUser类继承了这个类,并具有更多的性质。我正在使用MSSQL数据库。我有以下三个问题:

  • (我的主要问题)我应该创建DbSet<PersonUser>DbSet<CompanyUser>还是创建DbSet<ApplicationUser>?
  • 创建数据库连接时,外键应该指向PersonUserCompanyUser还是ApplicationUser?
  • 在控制器中,当我使用UserManager<TUser>SignInManager<TUser>时,取决于用户,我需要使用不同的属性。我必须有两个UserManagers还是我应该每次解析var user = (PersonUser) await this.userManager.GetUserAsync(this.User);?

下面是我的类:

public class ApplicationUser : IdentityUser, IAuditInfo, IDeletableEntity
{
public ApplicationUser()
{
this.Id = Guid.NewGuid().ToString();
this.Roles = new HashSet<IdentityUserRole<string>>();
this.Claims = new HashSet<IdentityUserClaim<string>>();
this.Logins = new HashSet<IdentityUserLogin<string>>();
this.Notifications = new HashSet<Notification>();
}
// Audit info
public DateTime CreatedOn { get; set; }
public DateTime? ModifiedOn { get; set; }
// Deletable entity
public bool IsDeleted { get; set; }
public DateTime? DeletedOn { get; set; }
public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
public virtual ICollection<IdentityUserClaim<string>> Claims { get; set; }
public virtual ICollection<IdentityUserLogin<string>> Logins { get; set; }
[Required]
public string Password { get; set; }
public UserType UserType { get; set; }
public virtual ICollection<Notification> Notifications { get; set; }
}
public class PersonUser : ApplicationUser
{
public PersonUser() : base()
{
this.Bids = new HashSet<Bid>();
this.Buildings = new HashSet<Building>();
}
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
public DesignerType DesignerType { get; set; }
public virtual ICollection<Bid> Bids { get; set; }
public virtual ICollection<Building> Buildings { get; set; }
}
public class CompanyUser : ApplicationUser
{
public CompanyUser() : base()
{
}
[Required]
public string CompanyName { get; set; }
// That is like Id of the company
[Required]
public string Bulstat { get; set; }
}

有两种不同范围的问题与不同的"类型"有关。

权限/授权

这是允许的不同类型的用户的范围要做的事情。它们可以调用哪些端点?他们被允许访问什么?

对于这个问题范围,您希望使用IdentityRoles来区分所述用户

<标题>

数据库关系当不同类型的用户在数据库中具有到不同表的真正不同的链接时,此范围。例如,对于你的SchoolDatabase,你可能有TeacherUsersStudentUsers有一对多的关系,反之亦然,但是两种类型的用户都可以登录。

在这种情况下,您实际上希望有不同的类来区分它们继承自User基类。

表链接两种类型的用户共享,例如,可能TeacherStudent都有它们所属的Classroom,您将放在基类中。也有机会像密码,登录名,用户名,电子邮件等都将在BaseUser类,因为每个人都有这样的东西。

表链接是*唯一的to one type of user would go on that users table. So if Teachers have a课程',那么你将添加一个链接在Teacher<->Curriculum之间,而不是BaseUser<->Curriculum,如果学生有家庭作业(但教师没有),那么该链接将专门指向Student表。

相关内容

最新更新