Search code examples
c#asp.net-identityasp.net-core-mvc

Error when instantiating a RoleManager to add roles to database


Trying to add a role to the database following this: Creating Roles in Asp.net Identity MVC 5

and/or this: Add role in ASP.NET Identity

I don't want Claims based authorization, just Roles, so I want to create a role using the Microsoft.AspNetCore.Identity.RoleManager.

The following code:

    public static class InitializeDb
    {
        public static void Initialize(IServiceProvider services)
        {
            using (var context = new ApplicationDbContext(services.GetRequiredService<DbContextOptions<ApplicationDbContext>>()))
            {
                using (var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context)))
                {
                   ...
                }
            }
        }
    }

produces the following error:

There is no argument given that corresponds to the required formal parameter 'roleValidators' of 'RoleManager.RoleManager(IRoleStore, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, ILogger>, IHttpContextAccessor)' SmartNetWorker..NETCoreApp,Version=v1.0

Apparently I have to supply the roleValidators parameter... or? Am I missing something?

Google says nothing.


Solution

  • Try resolve RoleManager:

    public static void Initialize(IServiceProvider services)
    {
        using (var roleManager = services.GetRequiredService<RoleManager<IdentityRole>>())
        {
            ...
        }
    }