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

时间:2019-02-25 14:16:05

标签: c# asp.net-core

当我以调试模式运行应用程序时,由于缺少服务,我的种子方法失败。错误消息是:

  

没有注册类型为'Microsoft.AspNetCore.Identity.RoleManager`1 [Microsoft.AspNetCore.Identity.IdentityRole]的服务。

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

RolesConfig.cs

public static class RolesConfig
{

    public static async Task InitialiseAsync(ApplicationDbContext context, IServiceProvider serviceProvider)
    {
        var roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
        string[] roleNames = {"Admin", "Report", "Search"};
        foreach (var roleName in roleNames)
        {
            var roleExist = await roleManager.RoleExistsAsync(roleName);
            if (!roleExist)
                await roleManager.CreateAsync(new IdentityRole(roleName));
        }
    }
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddHttpClient();
        services.AddHttpClient<IExceptionServiceClient, ExceptionServiceClient>();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

4 个答案:

答案 0 :(得分:2)

您最有可能需要添加

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

在您的ConfigureServices中的Startup.cs方法中

答案 1 :(得分:1)

在您的AddDefaultIdentity方法中,您已经在调用RoleManager,这是2.1中的新增功能,它在没有角色支持的情况下支持Identity。要添加角色支持,并因此向您的服务集合添加services.AddDefaultIdentity<IdentityUser>() .AddEntityFrameworkStores<ApplicationDbContext>() .AddRoles<IdentityRole>(); ,请如下修改您的代码:

{{1}}

答案 2 :(得分:1)

我认为您错过了AddRoleManager调用。这是我的类似设置,请尝试:

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

答案 3 :(得分:1)

您必须更改默认身份

services.AddIdentity<IdentityUser, IdentityRole>()