无法创建 Identity 类 的对象(UserManager 和 RoleManager)
Can not create objects of Identity classes (UserManager and RoleManager)
我正在开发一个 3 层架构应用程序,因此我在数据访问层将 UserManager 和 RoleManager 添加到我的 UnitOfWork。
但是当我尝试创建 UserManager 和 RoleManager classes 的对象时,我得到了这个错误:
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 的一部分 class
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;
}
}
更新
当我尝试创建自己的 RoleManager
和 UserManager
class 时,我遇到了同样的错误。
我的ApplicationRole
class
public class ApplicationRole : IdentityRole
{
}
我的ApplicationRoleManager
class
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(RoleStore<ApplicationRole> store)
: base(store)// error in a here (in a base)
{
}
}
您已将身份服务添加到 IoC 容器。所以你可以像这样在 UnitOfWork 的构造函数中使用依赖注入:
public class IdentityUnitOfWork : IUnitOfWork
{
private UserManager<IdentityUser> _userManager;
private RoleManager<IdentityRole> _roleManager;
private ApplicationContext _context;
public IdentityUnitOfWork(ApplicationContext context,
UserManager<IdentityUser> userManager,
RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
_context = context;
}
}
我正在开发一个 3 层架构应用程序,因此我在数据访问层将 UserManager 和 RoleManager 添加到我的 UnitOfWork。 但是当我尝试创建 UserManager 和 RoleManager classes 的对象时,我得到了这个错误:
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 的一部分 class
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;
}
}
更新
当我尝试创建自己的 RoleManager
和 UserManager
class 时,我遇到了同样的错误。
我的ApplicationRole
class
public class ApplicationRole : IdentityRole
{
}
我的ApplicationRoleManager
class
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
public ApplicationRoleManager(RoleStore<ApplicationRole> store)
: base(store)// error in a here (in a base)
{
}
}
您已将身份服务添加到 IoC 容器。所以你可以像这样在 UnitOfWork 的构造函数中使用依赖注入:
public class IdentityUnitOfWork : IUnitOfWork
{
private UserManager<IdentityUser> _userManager;
private RoleManager<IdentityRole> _roleManager;
private ApplicationContext _context;
public IdentityUnitOfWork(ApplicationContext context,
UserManager<IdentityUser> userManager,
RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
_context = context;
}
}