Car*_*nez 6 asp.net asp.net-mvc asp.net-identity asp.net-identity-2
所以,我正在尝试在我的应用程序上实现不同类型的用户,首先,假设只有一种用户:
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; }
}
Run Code Online (Sandbox Code Playgroud)
这两个实体之间存在一对一的关系,但是如果有多种类型的用户呢?我不能在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; }
}
Run Code Online (Sandbox Code Playgroud)
我的意思是,我可以扩展IdentityUserRole类并添加更多属性,但是如何为每种角色添加属性?
为此目的使用角色当然是有意义的,但这确实意味着您可以分配多个角色。因此用户可以是教师和学生,但这可能会发生。
如果您想向角色类添加额外的属性,其操作方式与用户的操作方式相同。创建您自己的版本,Role如下所示:
public class ApplicationRole : IdentityRole
{
public string bool CanJuggle { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并且您需要一个 RoleManager 类来配合它:
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(IRoleStore<ApplicationRole> store)
: base(store)
{ }
//snip
}
Run Code Online (Sandbox Code Playgroud)
不要忘记您的环境需要改变:
public class YourContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
//snip
}
Run Code Online (Sandbox Code Playgroud)
认为涵盖了所有相关部分。
| 归档时间: |
|
| 查看次数: |
1386 次 |
| 最近记录: |