在 Net Core 3.1 中,ASP.NET Core Identity 有两种不同的重载,第一个版本名为 DefaultIdentity,您没有机会同时设置用户和角色,因此语法如下
services.AddDefaultIdentity<IdentityUser>(options => ...
身份的第二个版本使用用户和角色,看起来像
services.AddIdentity<IdentityUser, IdentityRole>(options => ...
这是身份的不同版本,第一个包含 IdentityUI,第二个不包含 IdentityUI。
但是,如果您将角色服务添加为
services.AddDefaultIdentity<IdentityUser>(options =>...).AddRoles<IdentityRole>()...
如果您已将角色服务包含到您的服务集中,则可以将角色注入到配置方法中,如下所示
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DbContextOptions<ApplicationDbContext> identityDbContextOptions, UserManager<IdentityUser> userManager, RoleManager<IdentityRole> roleManager)
如果您注入,通常会出现此错误消息
RoleManager<IdentityRole> roleManager
在项目的任何位置,无需添加 IdentityRole 服务(通过第一种或第二种方式)。