Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController'

前端 未结 7 1571
灰色年华
灰色年华 2020-12-05 01:53

I\'m getting this error in Login Controller.

InvalidOperationException: Unable to resolve service for type \'Microsoft.AspNetCore.Identity.UserManager

7条回答
  •  一整个雨季
    2020-12-05 02:00

    Just to be clear about the answer:

    If you use the class ApplicationUser in startup.cs: services.AddIdentity()

    then you must use the same class in your controller when injecting it:

    public AccountController(UserManager userManager)
    

    If you use some other class such as:

    public AccountController(UserManager userManager)
    

    then you will get this error:

    InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[IdentityUser]'

    because you used ApplicationUser in startup, not IdentityUser so this type is not registered with the injection system.

提交回复
热议问题