注入RoleManager会导致错误 [英] Injecting RoleManager results in error
问题描述
首先,我的问题类似于此 SO链接,但不相同
First things first, my issue is similar to this SO link, but not the same
我正在查看Microsoft的Identity示例。我仅通过用户身份验证启动了它,并且起作用了。我删除了数据库,因为我想先使用数据库。现在,我正在尝试合并角色。
I am going through the Identity sample from microsoft. I started it with only user authentication and it worked. I removed the DB because I want to use Database first. Now I am trying to incorporate Roles.
我的设置是针对用户(没有角色的作品)和角色(没有作品)的:
My setup is like this for user (works without role) and role (didn't work):
我的数据库实体是这样设置的:
My DB entities are setup as such:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(e =>
{
e.ToTable("User").HasKey(x => x.Id);
e.Property(x => x.Id).HasColumnName("UserId");
});
builder.Entity<IdentityRole<int>>(e =>
{
e.ToTable("Role").HasKey(x => x.Id);
e.Property(x => x.Id).HasColumnName("RoleId");
});
和我的上下文,用户设置(我想继续使用原始的IdentityRole)
and my context, user setup (I want to keep using the original IdentityRole)
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
}
public class ApplicationUser : IdentityUser<int>
{
}
在ConfigureServices中:
in ConfigureServices:
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
在AccountController中,我尝试过:
in AccountController, I tried this:
[Authorize]
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly IEmailSender _emailSender;
private readonly ISmsSender _smsSender;
private readonly ILogger _logger;
public AccountController(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ISmsSender smsSender,
ILoggerFactory loggerFactory)
{
_userManager = userManager;
_roleManager = roleManager;
_signInManager = signInManager;
_emailSender = emailSender;
_smsSender = smsSender;
_logger = loggerFactory.CreateLogger<AccountController>();
}
该应用仍在浏览器中加载,它记得我之前已经登录过尝试注销,例如,出现此错误:
The app still load on the browser and it remembers that I've logged in before but when I try to logout for example, I get this error:
InvalidOperationException:无法解析
类型的服务'Microsoft.AspNetCore.Identity.RoleManager` 1 [MyProj.Web.Models.MyViewModels.ApplicationRole]'
,同时尝试激活
'MyProj.Web.Controllers.AccountController'。
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[MyProj.Web.Models.MyViewModels.ApplicationRole]' while attempting to activate 'MyProj.Web.Controllers.AccountController'.
推荐答案
将 RoleManager< IdentityRole>
替换为 RoleManager< IdentityRole< int>> ;
,它应该可以工作。
Replace RoleManager<IdentityRole>
by RoleManager<IdentityRole<int>>
and it should work.
这篇关于注入RoleManager会导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!