6

So, I'm trying to implement different kind of users on my application, first, let's say there's only one kind of user:

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; }
}

There's a one to one relationship between those 2 entities, but what if there's more than one type of user? I can't have that ForeignKey on the User entity, I think I'm going in the wrong direction.

I though about using roles for this, so there's an Admin, a Teacher, an Student, and different kind of roles for each one, but what happens if I want to store extra properties for each kind of role?

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; }
}

I mean, I can extend the class IdentityUserRole and add more properties, but how do I add properties for each kind of role?

Carlos Martinez
  • 4,350
  • 5
  • 32
  • 62
  • You can't add properties to each kind of roles seperately. All roles will contain all the properties you included in your UserRole table as each property will be a column, but you can assign different responsibilities to different roles. Or You could use claim based authentication. Check these videos in order to create custom roles. http://stackoverflow.com/questions/25857806/extending-identityuserrole-in-identity-2-0/25857923#25857923 – DSR Oct 20 '14 at 21:42

1 Answers1

4

It certainly makes sense to use roles for this purpose, but it does mean you could assign multiple roles. so a user could be a Teacher and a Student, but that can happen.

If you want to add extra properties to the role class, it's done in the same way as is done for user. Create your own version of Role like this:

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

And you need a RoleManager class to go with it:

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

    //snip
}

And not forgetting your context needs to change:

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

Think that covers all the relevant parts.

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    I'm still confused with this. I want each type of user to store different properties, so I can have a Teacher role, a Coordinator role, etc and each one has its task. – Carlos Martinez Oct 20 '14 at 21:25