I try to access usermanager in order to find a user. But struggling to figure out how this should be done. Here is what I have so far:
ApplicationDbContext
:
public class ApplicationDbContext : IdentityDbContext<IdentityUser, IdentityRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(AppDomain.CurrentDomain.BaseDirectory + "appsettings.json")
.Build();
var connectionString = configuration.GetConnectionString("AppDb");
optionsBuilder.UseSqlServer(connectionString);
}
}
Program.cs
:
var connectionString = builder.Configuration.GetConnectionString("AppDb");
builder.Services.AddDbContext<ApplicationDbContext>(x => x.UseSqlServer(connectionString));
LoginController
:
public class LoginController : Controller
{
private readonly UserManager<IdentityUser> _userManager;
public LoginController(UserManager<IdentityUser> userManager)
{
_userManager = userManager;
}
public IActionResult Index()
{
var user = _userManager.FindByEmailAsync("user@home.se");
//return View();
return null;
}
}
I get an error in LoginController
when initiate the LoginController
:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'WebSite.Controllers.LoginController'.
What am I doing wrong here?