C# 如何在ASP.NET Core的Startup.cs中注册RoleManager

C# 如何在ASP.NET Core的Startup.cs中注册RoleManager,c#,asp.net-core,C#,Asp.net Core,在调试模式下运行应用程序时,由于“缺少”服务,种子方法失败。错误消息是: 尚未注册类型为“Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.Identity.IdentityRole]”的服务 有人能帮我在StartUp.cs类中正确注册此服务吗?谢谢 角色配置 公共静态类角色配置 { 公共静态异步任务InitialiseAsync(ApplicationDbContext上下文,IServiceP

在调试模式下运行应用程序时,由于“缺少”服务,种子方法失败。错误消息是:

尚未注册类型为“Microsoft.AspNetCore.Identity.RoleManager`1[Microsoft.AspNetCore.Identity.Identity.IdentityRole]”的服务

有人能帮我在
StartUp.cs
类中正确注册此服务吗?谢谢

角色配置
公共静态类角色配置
{
公共静态异步任务InitialiseAsync(ApplicationDbContext上下文,IServiceProvider serviceProvider)
{
var rolemager=serviceProvider.GetRequiredService();
字符串[]roleNames={“Admin”、“Report”、“Search”};
foreach(roleName中的变量roleName)
{
var roleExist=wait rolemager.RoleExistsAsync(roleName);
如果(!roleExist)
等待roleManager.CreateAsync(新的IdentityRole(roleName));
}
}
}
Startup.cs
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddDbContext(选项=>
options.UseSqlServer(
GetConnectionString(“DefaultConnection”);
services.AddDefaultIdentity()
.AddEntityFrameworkStores();
services.AddHttpClient();
services.AddHttpClient();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

您很可能需要添加

services.AddIdentity<IdentityUser, IdentityRole>(config =>
{
        config.Password.RequireNonAlphanumeric = false; //optional
        config.SignIn.RequireConfirmedEmail = true; //optional
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentity(配置=>
{
config.Password.RequireNonAlphanumeric=false;//可选
config.SignIn.RequireConfirmedEmail=true;//可选
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

Startup.cs中的
ConfigureServices
方法中,您已经在调用
ConfigureServices
方法中,调用了
AddDefaultIdentity
,这是2.1中的一个新添加,它在没有角色支持的情况下构建身份。要向您的服务集合添加角色支持,从而添加
rolemager
在上,按如下方式修改代码:

services.AddDefaultIdentity<IdentityUser>()
  .AddRoles<IdentityRole>()
  .AddEntityFrameworkStores<ApplicationDbContext>();
  
services.AddDefaultIdentity()
.AddRoles()
.AddEntityFrameworkStores();

我想您错过了AddRoleManager的呼叫。下面是我的类似设置,请尝试:

        services.AddIdentity<IdentityUser, IdentityRole>(o => {
            o.Password.RequiredLength = 8;
        })
        .AddRoles<IdentityRole>()
        .AddRoleManager<RoleManager<IdentityRole>>()
        .AddDefaultTokenProviders()
        .AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentity(o=>{
o、 Password.RequiredLength=8;
})
.AddRoles()
.AddRoleManager()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores();

您必须更改默认标识


services.AddIdentity()

您的
ConfigureServices
方法在哪里?假定您使用的是ASP.NET核心标识,您正在调用
services.AddDefaultIdentity()吗
anywhere?检查此项:如果不了解您是如何配置服务的,则无法回答此问题。一般来说,如果您遇到与服务相关的异常,这是第一个要查找的地方。@ChrisPratt我已经添加了Startup.cs,现在可以了!非常感谢。对于使用此答案的其他人,请不要忘记删除
services.AddDefaultIdentity
使用此方法,在尝试激活“Microsoft.AspNetCore.Identity.RoleManager 1[Microsoft.AspNetCore.Identity.Identity.IdentityRole]时,引发了此错误
InvalidOperationException:无法解析类型“Microsoft.AspNetCore.Identity.IdentityRole]”的服务“.
添加服务的顺序也很重要(在某些情况下)。
AddEntityFrameworkStores
应该在
AddRoles
services.AddDefaultIdentity<IdentityUser>()
  .AddRoles<IdentityRole>()
  .AddEntityFrameworkStores<ApplicationDbContext>();
  
        services.AddIdentity<IdentityUser, IdentityRole>(o => {
            o.Password.RequiredLength = 8;
        })
        .AddRoles<IdentityRole>()
        .AddRoleManager<RoleManager<IdentityRole>>()
        .AddDefaultTokenProviders()
        .AddEntityFrameworkStores<ApplicationDbContext>();