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

前端 未结 7 1575
灰色年华
灰色年华 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条回答
  •  萌比男神i
    2020-12-05 02:05

    This is a bit unrelated to the original post but since Google brings you here... if you are getting this error and are using:

    services.AddIdentityCore()
    

    Then you will need to manually register the stuff that AddIdentity does, which can be found here: https://github.com/aspnet/Identity/blob/feedcb5c53444f716ef5121d3add56e11c7b71e5/src/Identity/IdentityServiceCollectionExtensions.cs#L79

            services.AddHttpContextAccessor();
            // Identity services
            services.TryAddScoped, UserValidator>();
            services.TryAddScoped, PasswordValidator>();
            services.TryAddScoped, PasswordHasher>();
            services.TryAddScoped();
            services.TryAddScoped, RoleValidator>();
            // No interface for the error describer so we can add errors without rev'ing the interface
            services.TryAddScoped();
            services.TryAddScoped>();
            services.TryAddScoped>();
            services.TryAddScoped, UserClaimsPrincipalFactory>();
            services.TryAddScoped>();
            services.TryAddScoped>();
            services.TryAddScoped>();
    

    You'll need to replace TUser and TRole with your implementations of those, or the default IdentityUser, IdentityRole

提交回复
热议问题