Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ASP.NET Core Dependency Injection error: Unable to resolve service for "Identity User" type while attempting to activate "Identity User"

Ask Question

The Errormessage is this:

The path /account/register threw an exception System.InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.IUserStore 1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'Microsoft.AspNetCore.Identity.UserManager 1[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);
                Does this answer your question? Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController'
– Nemanja Todorovic
                Feb 24, 2020 at 13:35

Ok, so in the startup class I changed:

services.AddIdentity<IdentityUser, IdentityRole>();
services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<AppDbContext>();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.