addidentity服务,用户经理

时间:2019-04-25 17:30:32

标签: c# asp.net-core entity-framework-core asp.net-identity identity

  

InvalidOperationException:尝试激活“ MyWebsite.Controllers.AccountController”时,无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager`1 [MyWebsite.Models.User]”的服务。

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

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();
        }
    }

班级用户

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;
        }

我是新来的,帮助

1 个答案:

答案 0 :(得分:0)

您要为services.AddDefaultIdentity<IdentityUser>()

添加服务

创建新的User模型

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

修改

确保您的IdentityRoles服务正常运行

services.AddDefaultIdentity<User, IdentityRole>()