Apporach #1:
The Claims approach may be the way to go. So you derive from IdentityUser
in the normal way and add the common properties to the derived class. Then for each type of user you would add extra claims using UserManager.AddClaimAsync
.
So, for example, let us say you create a new class user class called AppUser
. You can then do:
AppUser teacher = new AppUser { /* fill properties here */ };
/* Save User */
await userManager.AddClaimAsync(teacher.Id, new Claim("app_usertype", "teacher"));
await userManager.AddClaimAsync(teacher.Id, new Claim("app_grade", 4));
await userManager.AddClaimAsync(teacher.Id, new Claim("app_exp", 10));
await userManager.AddClaimAsync(teacher.Id, new Claim("app_awards", "Award1,Award2"));
await userManager.AddClaimAsync(teacher.Id, new Claim("app_langspoken", "English,French,German"));
AppUser student = new AppUser { /* fill properties here */ };
/* Save User */
await userManager.AddClaimAsync(student.Id, new Claim("app_usertype", "student"));
await userManager.AddClaimAsync(student.Id, new Claim("app_grade", 2));
These will add different claims to different types of users. So the teacher
claims to have "app_experience" of "10", "app_awards" of "Award1" and "Award2", etc. On the other hand the student
claims to only have "app_grade" of "2".
Basically, the first parameter identifies the type of the claim and the second parameter is the data that backs up that claim. The type can be anything, so choose something that makes sense to your application and maybe prefix each name to distinguish it from others. In the case I've just prefixed "app".
You can then use UserManager.GetClaimsAsync
to get all the claims for a user and search the returned list for the claim you are interested in.
Approach #2
The other approach would be to create a AppUser
class and then Teacher
and Student
class that is derived from AppUser
. In these classes you would add properties that would otherwise be added as claims in the example above.
The slight downside to this is you would have to create separate tables for each of these different users with relationships back to the ASP.NET Identity user table.
Plus, using FindByUserNameAsync
, FindByEmailAsync
, etc, will only ever return a single type of TUser
, in this case, AppUser
. Also, these methods will only query one table, AspNetUsers
, so it would be up to you to fetch the extra information from the relevant Teacher
or Student
table.