第一件事,我的問題是與此類似SO link,但不一樣的注漿RoleManager導致錯誤首次
我會通過微軟的身份樣本。我只用用戶身份驗證開始它,它工作。我刪除了數據庫,因爲我想先使用數據庫。現在我正在嘗試加入角色。
我的設置是這樣的用戶(無工作角色)和角色(沒有工作):
我的DB實體設置爲這樣:
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保持)
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>
{
}
public class ApplicationUser : IdentityUser<int>
{
}
在ConfigureServices:
services.AddIdentity<ApplicationUser, IdentityRole<int>>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
中的AccountController,我嘗試這樣做:
[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>();
}
的應用程序仍然加載瀏覽器和它記得,我在之前已經登錄,但是當我嘗試註銷例如,我得到這個錯誤:
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'.
我曾嘗試過,但配置錯誤,並記住,我已經嘗試過,並沒有奏效。謝謝! – user1019042
請注意,異常消息似乎暗示您正在使用'MyProj.Web.Models.MyViewModels.ApplicationRole'。如果這是真的,確保你不在代碼中的某處使用'IdentityRole'。 – Pinpoint