當我嘗試從資料庫中獲取所有用戶時收到此錯誤訊息 Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[xxx.Models.ApplicationUser]' 同時嘗試激活 'xxx.Areas.RegisteredUser.Controllers .AdminUserSetController'。這是代碼
AdminUserSetController.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; }
}
ApplicationDBContext.cs
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :base(options)
{
}
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
public DbSet<Company> Companies { get; set; }
}
public AdminUserSetController(UserManager usrMgr, IPasswordHasher passwordHash) 我認為問題出在這里。但我不知道該怎么做。請幫忙
uj5u.com熱心網友回復:
嘗試激活“xxx.Areas.RegisteredUser.Controllers.AdminUserSetController”時無法決議型別“Microsoft.AspNetCore.Identity.UserManager`1[xxx.Models.ApplicationUser]”的服務。
從錯誤訊息中我們可以看到問題,在您的 AdminUserSetController 中,您使用ApplicationUser
private UserManager<ApplicationUser> userManager;
private IPasswordHasher<ApplicationUser> passwordHasher;
但是在 Program.cs 中,您注冊IdentityUser
builder.Services
.AddIdentity<IdentityUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
您需要更改代碼,例如:
builder.Services
.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
并對您的背景關系進行一些更改:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :base(options)
{
}
閱讀自定義用戶資料以了解更多資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/530291.html
