[英]Use AspNetCore.Identity RoleManager and UserManager in an Interface
[英]Can not create objects of Identity classes (UserManager and RoleManager)
我正在開發一個 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 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
類時,我得到了同樣的錯誤。
我的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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.