如何在ASP.NET Core中添加多个身份和多个角色 [英] How to add multiple identity and multiple role in asp.net core
问题描述
我是asp.net核心的新手.因此,这可能不是一个好问题.
I am new to asp.net core. So this might not be a good question.
我有3个班级: Admin
, Doctor
, Patient
.
我希望所有这些用户都可以登录并查看其仪表板.它们都将具有不同的访问级别.
I want all those users to be able to login and see their dashboard. All of them will have different level of access.
所以我像这样从 IdenetityUser
派生了这些类:
So I derived these classes from IdenetityUser
like this:
public class Admin : IdentityUser
{
}
public class Doctor : IdentityUser
{
public const int MaxAppointPerDay = 28;
public Gender Gender { get; set; }
}
public class Patient : IdentityUser
{
public DateTime DateOfBirth { get; set; }
public Gender Gender { get; set; }
}
在这之后我只是被困住了.
I am just stuck after this.
当我尝试通过 AddIdentity< TUser,TRole>()
方法添加多个身份时,它给了我错误.
When I tried to add multiple Identitiy by the method AddIdentity<TUser, TRole>()
, it gave me error.
接下来我要做什么?
Internet上的大多数示例都涉及一个 IdentityUser
和多个 IdentityRole
.但是由于用户模型各不相同,所以我不能那样做.
Most of the examples in the internet involves one IdentityUser
and multiple IdentityRole
. But since the user models are all different, I couldn't go that way.
谢谢.
推荐答案
您不能反复使用 AddIdentity
添加身份.
You cannot repeatedly use AddIdentity
to add an identity.
ASP.NET Core提供了一个内置方法: AddIdentityCore< TUser>
.
ASP.NET Core provides a built-in method:AddIdentityCore<TUser>
.
您可以像这样使用它: services.AddIdentityCore< Admin>();
You can use it like this: services.AddIdentityCore<Admin>();
在银球的答案中,对于将来的读者来说,如果您使用此 AddIdentityCore
方法,则 AspNetUsers
表将合并每个 IdentityUser 的所有属性代码>.这是一个示例:
Adding to Yinqiu's answer, for the future readers, if you use this AddIdentityCore
method, the AspNetUsers
table will merge all the properties of each IdentityUser
. Here is an example:
让 Teacher
和 Student
是两个 IdentityUser
,它们都具有至少一个不同的属性,例如:
Let Teacher
and Student
are two IdentityUser
and both of them have at least one distinct properties, like this:
public class Teacher : IdentityUser
{
public string Subject { get; set; }
}
public class Student : IdentityUser
{
public int Grade { get; set; }
}
然后我将它们的两个身份添加到 startup.cs
中,如下所示:
Then I add both of their Identity in the startup.cs
like this:
services.AddIdentityCore<Teacher>().AddEntityFrameworkStores<AppDbContext>();
services.AddIdentityCore<Student>().AddEntityFrameworkStores<AppDbContext>();
AspNetUsers 表将同时包含 Subject
属性和 Grade
属性.这是迁移类的证明:
The AspNetUsers table will include both the Subject
property and the Grade
property. Here is the proof from migration class:
migrationBuilder.CreateTable(
name: "AspNetUsers",
columns: table => new
{
Id = table.Column<string>(nullable: false),
UserName = table.Column<string>(maxLength: 256, nullable: true),
NormalizedUserName = table.Column<string>(maxLength: 256, nullable: true),
Email = table.Column<string>(maxLength: 256, nullable: true),
NormalizedEmail = table.Column<string>(maxLength: 256, nullable: true),
EmailConfirmed = table.Column<bool>(nullable: false),
PasswordHash = table.Column<string>(nullable: true),
SecurityStamp = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true),
PhoneNumber = table.Column<string>(nullable: true),
PhoneNumberConfirmed = table.Column<bool>(nullable: false),
TwoFactorEnabled = table.Column<bool>(nullable: false),
LockoutEnd = table.Column<DateTimeOffset>(nullable: true),
LockoutEnabled = table.Column<bool>(nullable: false),
AccessFailedCount = table.Column<int>(nullable: false),
Discriminator = table.Column<string>(nullable: false),
Grade = table.Column<int>(nullable: true),
Subject = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetUsers", x => x.Id);
});
请注意,最后两个属性是 Grade
和 Subject
.
Notice the last two properties are Grade
and Subject
.
这类似于为所有 IdentitiyUser
使用基类,该基类将从 IdentityUser
派生并包含所有属性的组合,然后仅使用添加该基类> AddIdentity< TUser,TRole>
方法.
It's similar to using a base class for all the IdentitiyUser
which will derive from IdentityUser
and include combination of all propeties and then just adding that one base class using AddIdentity<TUser, TRole>
method.
这篇关于如何在ASP.NET Core中添加多个身份和多个角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!