簡體   English   中英

asp.net 核心身份來自 BaseApplicationUser 的多個用戶

[英]asp.net core identity Multiple users from BaseApplicationUser

我使用 asp.net core 2.2 Identity 作為我的用戶管理系統。 我需要有幾種類型的用戶......例如,倉庫用戶和應用程序用戶我創建了一個從 idntity 用戶類繼承的基類 => IdentityUser<long>

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

和倉庫用戶和存儲用戶從 BaseApplicationUser 繼承來創建不同的用戶。

   public class ApplicationUser:BaseApplicationUser 
    {
.
.
}
public class WarehouseApplicationUser:BaseApplicationUser 
        {
    .
    .
    }

我只想為所有人設置一張表 => "AspNetUsers" OnModelCreating 添加了以下代碼:

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

並在 Context 中的 DbSets 中:

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

** 我需要並為“ApplicationRole”和“WarehouseApplicationUsers”等用戶創建單獨的角色類

什么是問題? 1- 如何在 asp.net core Identity 中創建不同的用戶? (最好的方法)2-另一個問題是 => 當我將字段添加到“ApplicationUser”時,此字段成功添加到“AspNetUsers”,但是當將字段添加到“WarehouseApplicationUsers”EF Core 時,添加名稱為“WarehouseApplicationUsers”和“WarehouseApplicationRole”的新表和“WarehouseApplicationUserRole”然后在“WarehouseApplicationUsers”表中添加字段......這是什么!!!

最后,如何繼承和構建不同用戶的層次結構? 感恩

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

ASP.NET Core 提供了一個內置方法: AddIdentityCore<TUser>

您可以像這樣使用它: services.AddIdentityCore<ApplicationUser>();

您可以查看此線程的更多詳細信息。

問題2:

這是一個示例,您可以與您的代碼進行比較:

基本應用程序用戶:

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

申請用戶:

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

倉庫應用用戶:

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

應用程序數據庫上下文:

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

遷移結果: 在此處輸入圖片說明

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

像這樣創建所有模型:

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

在 Visual Studio 中右鍵單擊您的項目(不是解決方案)並選擇Open in Terminal ,然后鍵入以下行以遷移和創建數據庫:

dotnet ef migrations add createDB -o data/migrations
dotnet ef database update

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM