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

前端 未结 7 1537
灰色年华
灰色年华 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<ApplicationUser, IdentityRole>()

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

    public AccountController(UserManager<ApplicationUser> userManager)
    

    If you use some other class such as:

    public AccountController(UserManager<IdentityUser> 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.

    0 讨论(0)
  • 2020-12-05 02:01

    You can set IdentityUser and IdentityRole in ConfigureServices inside Startup class individually as shown below:

    services.AddDefaultIdentity<IdentityUser>()
        .AddRoles<IdentityRole>()
        .AddEntityFrameworkStores<ApplicationDbContext>();
    

    OR

    you can configure directly into AddIdentity:

    services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>();
    
    0 讨论(0)
  • 2020-12-05 02:04

    You need to update your Statup.cs class with below

    services.AddIdentity<ApplicationUser,IdentityRole>() .AddEntityFrameworkStores();

    Here : ApplicationUser is my custom model class.

    0 讨论(0)
  • 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<YourAppUser>()
    

    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<IUserValidator<TUser>, UserValidator<TUser>>();
            services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
            services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
            services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
            services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
            // No interface for the error describer so we can add errors without rev'ing the interface
            services.TryAddScoped<IdentityErrorDescriber>();
            services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
            services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
            services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
            services.TryAddScoped<UserManager<TUser>>();
            services.TryAddScoped<SignInManager<TUser>>();
            services.TryAddScoped<RoleManager<TRole>>();
    

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

    0 讨论(0)
  • 2020-12-05 02:06

    don't forget to add role manager in ConfigureServices

    services.AddDefaultIdentity<IdentityUser>()
        .AddRoles<IdentityRole>() // <--------
        .AddDefaultUI(UIFramework.Bootstrap4)
        .AddEntityFrameworkStores<ApplicationDbContext>();
    
    0 讨论(0)
  • 2020-12-05 02:18

    You need to use the same user data model in SignInManager, UserManager and services.AddIdentity. Same principal is true if you are using your own custom application role model class.

    So, change

    services.AddIdentity<IdentityUser, IdentityRole>(options =>
        {
            options.User.RequireUniqueEmail = false;
        })
        .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
        .AddDefaultTokenProviders();
    

    to

    services.AddIdentity<Automobile.Models.Account, IdentityRole>(options =>
        {
            options.User.RequireUniqueEmail = false;
        })
        .AddEntityFrameworkStores<Providers.Database.EFProvider.DataContext>()
        .AddDefaultTokenProviders();
    
    0 讨论(0)
提交回复
热议问题