addidentity 服务问题,usermanager

Problem with addidentity services, usermanager

InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MyWebsite.Models.User]' while attempting to activate 'MyWebsite.Controllers.AccountController'.

访问/帐户/注册时出现此错误

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>()
                .AddDefaultUI(UIFramework.Bootstrap4)
                .AddEntityFrameworkStores<ApplicationDbContext>();

            //services.AddIdentity<User, IdentityRole>()
            //.AddEntityFrameworkStores<ApplicationDbContext>();0

ApplicationDbContext

public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            Database.EnsureCreated();
        }
    }

Class 用户

public class User : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
    }

账户管理员

private readonly UserManager<User> _userManager;
        private readonly SignInManager<User> _signInManager;
        private readonly RoleManager<IdentityRole> _roleManager;

        public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _roleManager = roleManager;
        }

我是新人,求助

您正在为 services.AddDefaultIdentity<IdentityUser>()

添加服务

当您创建了一个新的 User 模型时

因此您必须为该模型添加服务 services.AddDefaultIdentity<User>()

编辑

确保 IdentityRoles 服务正常工作

services.AddDefaultIdentity<User, IdentityRole>()