无法创建UserManager和RoleManager类的对象(身份)

问题描述 投票:0回答:1

我正在开发3层体系结构应用程序,因此我将UserManager和RoleManager添加到数据访问层的UnitOfWork中。但是,当我尝试创建UserManager和RoleManager类的对象时,出现此错误:

There is no argument given that corresponds to the required formal parameter 'optionsAccessor' 
of 'UserManager<IdentityUser>.UserManager(IUserStore<IdentityUser>, IOptions<IdentityOptions>, 
IPasswordHasher<IdentityUser>, IEnumerable<IUserValidator<IdentityUser>>, 
IEnumerable<IPasswordValidator<IdentityUser>>, ILookupNormalizer, IdentityErrorDescriber, 
IServiceProvider, ILogger<UserManager<IdentityUser>>)'

There is no argument given that corresponds to the required formal parameter 'roleValidators'
 of 'RoleManager<IdentityRole>.RoleManager(IRoleStore<IdentityRole>, IEnumerable<IRoleValidator<IdentityRole>>, 
ILookupNormalizer, IdentityErrorDescriber, ILogger<RoleManager<IdentityRole>>)'

我的UnitOfWork类的一部分

    public class IdentityUnitOfWork : IUnitOfWork
    {
        private UserManager<IdentityUser> _userManager;
        private RoleManager<IdentityRole> _roleManager;
        private ApplicationContext _context;

        public IdentityUnitOfWork(ApplicationContext context)
        {
            _userManager = new UserManager<IdentityUser>(context);// error 1
            _roleManager = new RoleManager<IdentityRole>(context);// error 2
            _context = context;
        }
    }

c# asp.net-core asp.net-identity identity data-access-layer
1个回答
0
投票

代替直接将上下文传递给用户和角色管理器,您需要创建用户和角色存储,并像这样将上下文传递给用户和角色存储

 public class IdentityUnitOfWork : IUnitOfWork
    {
        private UserManager<IdentityUser> _userManager;
        private RoleManager<IdentityRole> _roleManager;
        private DbContext _context;

        public IdentityUnitOfWork(DbContext context)
        {
            _userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(context));
            _roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
            _context = context;
        }
    }

错误也建议使用用户存储和角色存储

© www.soinside.com 2019 - 2024. All rights reserved.