Search code examples
c#asp.net-coreasp.net-core-mvcuser-roles

Cannot get the UserManager class


What I'm trying to do is to add a new admin user and assign it with the admin role. So.. I went to the Startup.cs class in Configure method and wrote the following code:

var context = app.ApplicationServices.GetService<ApplicationDbContext>();

// Getting required parameters in order to get the user manager
var userStore = new UserStore<ApplicationUser>(context);

// Finally! get the user manager!
var userManager = new UserManager<ApplicationUser>(userStore);

However, I get the following error message:

Severity Code Description Project File Line Suppression State Error CS7036 There is no argument given that corresponds to the required formal parameter 'optionsAccessor' of UserManager.UserManager(IUserStore, IOptions, IPasswordHasher, IEnumerable>, IEnumerable>, ILookupNormalizer, IdentityErrorDescriber, IServiceProvider, ILogger>)' FinalProject..NETCoreApp,Version=v1.0 C:\Users\Or Natan\Documents\Visual Studio 2015\Projects\FinalProject\src\FinalProject\Startup.cs 101 Active

This error is killing me here.. obviously I need the userManager in order to create the new user but I just can't initialize this thing.


Solution

  • You can use dependency injection to obtain an instance of UserManager. Just add a parameter to the configure method, like this:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
    

    Then you can create users and roles. I usually move this code into a static class...

    public static class DbInitializer
    {
        public static async Task Initialize(ApplicationDbContext context, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
        {
            // Ensure that the database exists and all pending migrations are applied.
            context.Database.Migrate();
    
            // Create roles
            string[] roles = new string[] { "UserManager", "StaffManager" };
            foreach (string role in roles)
            {
                if (!await roleManager.RoleExistsAsync(role))
                {
                    await roleManager.CreateAsync(new IdentityRole(role));
                }
            }
    
            // Create admin user
            if (!context.Users.Any())
            {
                await userManager.CreateAsync(new ApplicationUser() { UserName = "info@example.com", Email = "info@example.com" }, "p@ssw0rd");
            }
    
            // Ensure admin privileges
            ApplicationUser admin = await userManager.FindByEmailAsync("info@example.com");
            foreach (string role in roles)
            {
                await userManager.AddToRoleAsync(admin, role);
            }
        }
    }
    

    ... and call the method in the Startup.Configure method:

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContext context, UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
    {
        // Code omitted for brevity
    
        // Create seed data
        DbInitializer.Initialize(context, userManager, roleManager).Wait();
    }
    

    In one of the next releases of Entity Framework Core database seeding will be added as a feature.