InvalidOperationException: impossibile risolvere il servizio per il tipo "Microsoft.AspNetCore.Identity.UserManager`1 [MyWebsite.Models.User]" durante il tentativo di attivare "MyWebsite.Controllers.AccountController".
Ottengo questo errore quando accedo a / Account / Register
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
//services.AddIdentity<User, IdentityRole>()
//.AddEntityFrameworkStores<ApplicationDbContext>();0
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
Database.EnsureCreated();
}
}
Utente di classe
public class User : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
}
Account Controller
private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;
private readonly RoleManager<IdentityRole> _roleManager;
public AccountController(UserManager<User> userManager, SignInManager<User> signInManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_signInManager = signInManager;
_roleManager = roleManager;
}
Sono nuovo, aiuto
Stai aggiungendo un servizio per services.AddDefaultIdentity<IdentityUser>()
Dopo aver creato un nuovo modello User
Quindi devi aggiungere il servizio per quel modello di services.AddDefaultIdentity<User>()
AddDefaultIdentity services.AddDefaultIdentity<User>()
modificare
Assicurarsi che il servizio IdentityRoles
funzioni
services.AddDefaultIdentity<User, IdentityRole>()