gpt4 book ai didi

asp.net - ASP.Net Identity 2.0 中的不同用户类型

转载 作者:行者123 更新时间:2023-12-02 14:07:29 24 4
gpt4 key购买 nike

因此,我尝试在我的应用程序上实现不同类型的用户,首先,假设只有一种用户:

public class ApplicationUser : IdentityUser
{
// Other Properties
public int TeacherID { get; set; }

[ForeignKey("TeacherID ")]
public virtual Teacher Teacher { get; set; }
}

public class Teacher
{
[Key]
public int TeacherID { get; set; }
public int UserID { get; set; }
// Other properties

[ForeignKey("UserID")]
public virtual ApplicationUser User { get; set; }
}

这两个实体之间存在一对一的关系,但是如果有不止一种类型的用户怎么办?我不能在 User 实体上使用foreignkey,我想我走错了方向。

我想为此使用角色,因此有一个管理员、一个教师、一个学生,每个角色都有不同类型的角色,但是如果我想为每种角色存储额外的属性,会发生什么?

public class IdentityUserRole<TKey>
{
public IdentityUserRole();

// Resumen:
// RoleId for the role
public virtual TKey RoleId { get; set; }
//
// Resumen:
// UserId for the user that is in the role
public virtual TKey UserId { get; set; }
}

我的意思是,我可以扩展 IdentityUserRole 类并添加更多属性,但是如何为每种角色添加属性?

最佳答案

为此目的使用角色当然是有意义的,但这确实意味着您可以分配多个角色。因此用户可以是教师和学生,但这可能会发生。

如果您想向角色类添加额外的属性,其操作方式与用户的操作方式相同。创建您自己的 Role 版本,如下所示:

public class ApplicationRole : IdentityRole
{
public string bool CanJuggle { get; set; }
}

并且您需要一个 RoleManager 类来配合它:

public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole> store)
: base(store)
{ }

//snip
}

不要忘记您的环境需要改变:

public class YourContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
//snip
}

认为涵盖了所有相关部分。

关于asp.net - ASP.Net Identity 2.0 中的不同用户类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26474672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com