Problem with addidentity services, usermanager

asp.net-core asp.net-identity c# entity-framework-core identity

Question

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyWebsite.Models.User]' while attempting to activate 'MyWebsite.Controllers.AccountController'.

I get this error when accessing / Account / Register

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>()
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddEntityFrameworkStores<ApplicationDbContext>();

            //services.AddIdentity<User, IdentityRole>()
            //.AddEntityFrameworkStores<ApplicationDbContext>();0

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            Database.EnsureCreated();
        }
    }

Class User

public class User : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
    }

Account Controller

private readonly UserManager<User> _userManager;
        private readonly SignInManager<User> _signInManager;
        private readonly RoleManager<IdentityRole> _roleManager;

        public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _roleManager = roleManager;
        }

I'm new, help

1
1
4/25/2019 5:51:46 PM

Accepted Answer

You are adding a service for services.AddDefaultIdentity<IdentityUser>()

When you have created a new User Model

So you have to add the service for that Model services.AddDefaultIdentity<User>()

Edit

Making sure you have the IdentityRoles service working

services.AddDefaultIdentity<User, IdentityRole>()

0
4/25/2019 5:56:56 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow