当我试图从数据库获取所有用户时收到此错误消息。在试图激活“xxx.Areas.RegisteredUser.Controllers. AdminUserSetController”时,无法解析类型为“Microsoft.AspNetCore.Identity.UserManager ′ 1[xxx.Models.ApplicationUser]”的服务。以下是代码
管理员使用者设定控制器. cs
public class AdminUserSetController : Controller
{
private UserManager<ApplicationUser> userManager;
private IPasswordHasher<ApplicationUser> passwordHasher;
public AdminUserSetController(UserManager<ApplicationUser> usrMgr, IPasswordHasher<ApplicationUser> passwordHash) // <--- i think the problem is here
{
userManager = usrMgr;
passwordHasher = passwordHash;
}
public IActionResult Index() //<------ does not hit when load
{
return View(userManager.Users);
}
程序. cs
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("LifeConnection")
));
builder.Services.Configure<StripeSettings>(builder.Configuration.GetSection("Stripe"));
builder.Services
.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddScoped<iUnitOfWork, UnitOfWork>();
builder.Services.AddSingleton<IEmailSender, EmailSender>();
builder.Services.AddRazorPages();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Identity/Account/Login";
options.LogoutPath = $"/Identity/Account/Logout";
options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseDeveloperExceptionPage();
app.UseRouting();
StripeConfiguration.ApiKey = builder.Configuration.GetSection("Stripe:SecretKey").Get<string>();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
app.MapControllerRoute(
name: "default",
pattern: "{area=UnRegUser}/{controller=Home}/{action=Index}/{id?}");
app.Run();
应用程序使用者. cs
public class ApplicationUser : IdentityUser
{
[Required]
public string FullName { get; set; }
public string? UserType { get; set; }
public bool? Active { get; set; } = true;
public int? CompanyId { get; set; }
[ForeignKey ("CompanyId")]
[ValidateNever]
public Company Company { get; set; }
}
应用程序数据库上下文. cs
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :base(options)
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Company> Companies { get; set; }
}
我想问题出在这里,但是我不知道该怎么做,请帮助
1条答案
按热度按时间ttp71kqs1#
尝试激活“xxx.区域.注册用户.控制器.管理员用户集控制器”时,无法解析类型“Microsoft.AspNetCore.Identity.用户管理器”1[xxx.模型.应用程序用户]“的服务。
从错误信息中我们可以看出问题所在,在你的AdminUserSetController中,你使用了
ApplicationUser
但是在Program.cs中,您注册了
IdentityUser
您需要更改代码,如下所示:
并对上下文进行一些更改:
阅读自定义用户数据了解更多信息。