3

I am using ASP.NET Core 2.2 Identity for my user management system. I need to have several types of users ... for example, warehouse user and application user, which I create a base class that inherits from identity user class => IdentityUser<long>

public class BaseApplicationUser : IdentityUser<long>
{
    public string FirstName { set; get; }
    public string LastName { set; get; }
    ...
}

and warehouse user and store user inherit from BaseApplicationUser to create different users.

public class ApplicationUser : BaseApplicationUser
{
}

I want to have just one table for all of them => AspNetUsers on OnModelCreating added this code :

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<ApplicationUser>();
    builder.Entity<WarehouseApplicationUser>();
}

and in DbSets in Context :

public DbSet<ApplicationRole> ApplicationRole { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<WarehouseApplicationRole> WarehouseApplicationRole { get; set; }
public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }

I need to create separate role classes for the users, like ApplicationRole and WarehouseApplicationUsers.

Whats is the problem?

  1. How to create different users in asp.net core Identity? ( best way )
  2. Another problem is that when I add a field to ApplicationUser, that field is successfully added to AspNetUsers, but when I add a field to WarehouseApplicationUsers, EF Core adds new tables named WarehouseApplicationUsers and WarehouseApplicationRole and WarehouseApplicationUserRole and then adds a field to the WarehouseApplicationUsers table... what is this!

And finally, how can I inherit and build a hierarchy of different users? Thanks a lot

Burhan Savci
  • 376
  • 1
  • 4
  • 9
Jb_Dda
  • 41
  • 8
  • 1
    Just don't use `IdentityUser` in your application code. Create something like an `ApplicationUser` that only has a foreign key to the IdentityUser in the database. Keep things apart. The application has nothing to do with `IdentityUser`'s concerns, and vv. – Gert Arnold Dec 26 '21 at 16:00

2 Answers2

3

Problem 1: How to create different users in asp.net core Identity ?

ASP.NET Core provides a built-in method:AddIdentityCore<TUser>.

You can use it like this: services.AddIdentityCore<ApplicationUser>();

More details you can see this thread.

Problem 2:

Here is an example,you can compare to your code:

BaseApplicationUser:

 public class BaseApplicationUser: IdentityUser
{
    public string FirstName { set; get; }
    public string LastName { set; get; }
}

ApplicationUser:

 public class ApplicationUser : BaseApplicationUser
{
    public string Year { get; set; }
}

WarehouseApplicationUser:

public class WarehouseApplicationUser : BaseApplicationUser
{
     public string Age { get; set; }
}

ApplicationDbContext:

public class ApplicationDbContext : IdentityDbContext<BaseApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }
  
    public DbSet<ApplicationUser> ApplicationUsers { get; set; }
   
    public DbSet<WarehouseApplicationUser> WarehouseApplicationUsers { get; set; }
}

Migration result: enter image description here

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
-1

NetCore 6.0.1 and EfCore 6.0.1 :

DbContext :

using Attendance.Server.Data.Entities;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace Attendance.Server.Data;
// this line in important :
public class AppDbContext : IdentityDbContext<User, Role, int, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // AspNet Identity
        modelBuilder.Entity<User>(entity => { entity.ToTable("User"); });
        modelBuilder.Entity<Role>(entity => { entity.ToTable("Role"); });
        modelBuilder.Entity<UserRole>(entity => { entity.ToTable("UserRole"); });
        modelBuilder.Entity<RoleClaim>(entity => { entity.ToTable("RoleClaim"); });
        modelBuilder.Entity<UserClaim>(entity => { entity.ToTable("UserClaim"); });
        modelBuilder.Entity<UserLogin>(entity => { entity.ToTable("UserLogin"); });
        modelBuilder.Entity<UserToken>(entity => { entity.ToTable("UserToken"); });
    }
}

Create all models like this:

using Microsoft.AspNetCore.Identity;
namespace Attendance.Server.Data.Entities;
public class User : IdentityUser<int> { }

Do not forget install these packages on your web project:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design

Add this line in your program.cs :

builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

your appsettings.json :

"Logging": {
"LogLevel": {
  "Default": "Information",
  "Microsoft.AspNetCore": "Warning"
  }
},
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=.\\SQLEXPRESS;Initial Catalog=Attendance;Integrated Security=True;MultipleActiveResultSets=True"
  }

Finaly right click on your project (not solution) in Visual Studio and select Open in Terminal , then type these lines to migrate and create database:

dotnet ef migrations add createDB -o data/migrations
dotnet ef database update
M Komaei
  • 7,006
  • 2
  • 28
  • 34