skip to Main Content

I’m working on an ASP.NET Core Identity project where I have two distinct types of users: Students and Teachers. The requirement is as follows:

  • Student accounts can only log in to the Student site.
  • Teacher accounts can only log in to the Teacher site.
  • A Student account should not be able to log into the Teacher site, and vice versa. It can’t even found the username.

It is something like 2 login systems.

Code ER

public class ApplicationUser: IdentityUser
{

}

public class Student: ApplicationUser
{
   
}

public class Teacher: ApplicationUser
{

}

public class WorkNextDbContext: IdentityDbContext<ApplicationUser>
{
    public WorkNextDbContext(DbContextOptions options) : base(options)
    {
        
    }

    public virtual DbSet<Student> Students { get; set; }
    public virtual DbSet<Teacher> Teachers { get; set; }
}

Currently, I am using ASP.NET Core Identity, which generates both user types in the same aspnetusers table. This is causing an issue where usernames are duplicated between the two user types. I don’t think roles are appropriate in this scenario since these are two completely different user types with distinct login systems.

aspnetusers Table

Id Discriminator Username
1 Student Ali
2 Teacher Bob
3 Student Bob -Error, duplicate username, username must be unique, but identity core generate two type of user in 1 table

How can I set up two separate login systems while preventing these user types from overlapping in the same aspnetusers table and causing username conflicts? Is there a way to keep the Student and Teacher accounts entirely separate in the database or another workaround that aligns with Core Identity best practices?

2

Answers


  1. I think you should have everything separate for both the entities.

    Separate Tables and Contexts: Use different tables and contexts to avoid username duplication.

    Different Authentication Flows: Configure distinct login controllers and views.

    Custom Cookie Authentication Schemes: Prevent session conflicts by using separate cookies.

    Role-Based Policies: Restrict access to specific areas for students and teachers.

    This was it will make sure that two isolated login systems with no overlap in users.

    Login or Signup to reply.
  2. Based on my opinion it is better to use different table, login and identity options for both type which is student and teacher. For that you might need to modify the code little bit. even keep separate Authorization Policies.

     public class StudentDbContext : IdentityDbContext<Student>
       {
           public StudentDbContext(DbContextOptions<StudentDbContext> options)
               : base(options)
           {
           }
       }
    
       public class TeacherDbContext : IdentityDbContext<Teacher>
       {
           public TeacherDbContext(DbContextOptions<TeacherDbContext> options)
               : base(options)
           {
           }
       }
    

    Set the user type column in the tables to show if the user is teacher or student. And to avoid the username conflict issue use the email id which will be unique for the login or create unique username by using custom code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search