How to identify different types of users in Identity ASP.NET?

In my application, I use ASP.NET Identity, and I have different types of users (Teacher, Student, ...) that have their own properties. For for Teacher I have Experience, Languages Spoken, Certifications, Awards, Affiliations, ... , and for Student I have different properties, Thus, I can not use roles due to different information for each user. So all of them are actually users, I mean that they can log in to my site. They also have general information for registration: First Name, Last Name, Email, Password Now, what do you think and what is the best option for this? Should I create a class for each user that inherits from IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim> ? Any idea?

PS : I found several solutions for this, for example, it is recommended to use claims here. But this is not clear enough for me. Actually, claims are a difficult part of the puzzle, and I did not understand what they are? :), It would be better to have examples. thanks

+7
asp.net-mvc asp.net-identity-2
source share
2 answers

Apporach # 1:

A claim approach can be a way. This way you get from IdentityUser usual way and add common properties to the derived class. Then, for each type of user, you add additional claims using UserManager.AddClaimAsync .

So, for example, let's say you create a class class of the new AppUser class. Then you can:

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

They will add different requirements to different types of users. Thus, the teacher claims to have "app_experience" of "10", "app_awards" of "Award1" and "Award2", etc. On the other hand, student claims to have only "app_grade" from "2".

Basically, the first parameter determines the type of claim, and the second parameter determines the data that supports this claim. The type can be any, so choose what makes sense for your application, and perhaps the prefix of each name to distinguish it from others. In the case where I only prefix "application".

Then you can use UserManager.GetClaimsAsync to receive all claims against the user and search the returned list of the application you are interested in.

Approach # 2

Another approach would be to create an AppUser class, followed by teacher and student , which is derived from AppUser . In these classes, you would add properties that would otherwise be added as claims in the example above.

The slight drawback to this is that you will have to create separate tables for each of these different users, and the relationship back to the ASP.NET user identifier table.

Also using FindByUserNameAsync , FindByEmailAsync etc. will only return one type of TUser , in this case AppUser . In addition, these methods will query only one table, AspNetUsers , so you will need to extract additional information from the corresponding teacher or student table.

+8
source share

Since all users will have the same properties, it would be wise to create a User class to store all properties that will be the same for teachers, students, etc.

Then I created a class for each type of user, using only those properties that apply to this type of user. In this class, I would include UserId as one of the properties so that you have relationships from your main group to their individual types. See below:

User Class: UserId (primary key), First Name, Last Name, Log In, Password, Etc.

Teacher Class: UserId (foreign key), Class Teached, Experience, Awards, Etc.

Student Class: UserId (Foreign Key), Grade, Honors, Etc.

There are many ways to do what you do, so this is just one suggestion. Good luck

+1
source share

All Articles