0

The Errormessage is this:

The path /account/register threw an exception System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager1[Microsoft.AspNetCore.Identity.

I can't figure out what causes this. Any help is greatly appreciated, thank you!

    public class AccountController : Controller
    {
        private readonly UserManager<IdentityUser> userManager;
        private readonly SignInManager<IdentityUser> signInManager;

        public AccountController(UserManager<IdentityUser> userManager,
                                 SignInManager<IdentityUser> signInManager)
        {
            this.userManager = userManager;
            this.signInManager = signInManager;
        }

       [HttpGet]
        public IActionResult Register()
        {

            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Register(RegisterViewModel model)
        {
            if(ModelState.IsValid)
            {
                var user = new IdentityUser { UserName = model.Email, Email = model.Email };
                var result = await userManager.CreateAsync(user, model.Password);

                if(result.Succeeded)
                {
                    // Persistend creates either session or permanent cookie
                    await signInManager.SignInAsync(user, isPersistent: false);
                    return RedirectToAction("index", "home");
                }

                foreach (var error in result.Errors)
                {
                    // Adding this to Modelstate goes to validation-summary in register.cshtml
                    ModelState.AddModelError("", error.Description);
                }
            }
            return View(model);
        }
    }

Timo B.
  • 21
  • 1
  • 3
  • Does this answer your question? [Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager\` while attempting to activate 'AuthController'](https://stackoverflow.com/questions/44483589/unable-to-resolve-service-for-type-microsoft-aspnetcore-identity-usermanager-w) – Nemanja Todorovic Feb 24 '20 at 13:35

1 Answers1

1

Ok, so in the startup class I changed:

services.AddIdentity<IdentityUser, IdentityRole>();

to

services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
Timo B.
  • 21
  • 1
  • 3