I dont know how can I get the login user details in my controller. from Google I applied the following code but the error is coming as given below
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MetaWeather.Models.ApplicationUser]' while attempting to activate 'MetaWeather.Areas.Customer.Controllers.ForcastController'.
[Area("Customer")]
[Authorize]
public class ForcastController : Controller
{
private readonly IWeatherRepository _weatherRepository;
private readonly IUnitOfWork _unitOfWork;
private readonly UserManager<ApplicationUser> _userManager;
public Weather Weather { get; set; }
public ForcastController(IWeatherRepository weatherRepository, IUnitOfWork unitOfWork,
UserManager<ApplicationUser> userManager)
{
_weatherRepository = weatherRepository;
_unitOfWork = unitOfWork;
_userManager = userManager;
}
public async Task<IActionResult> Index()
{
// ApplicationUser applicationUser = await _userManager.GetUserAsync(User);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
var user = await _userManager.FindByIdAsync(userId);
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
410 Points
1326 Posts
Not getting the login user details from my controller
Jun 04, 2020 07:25 PM|polachan|LINK
I dont know how can I get the login user details in my controller. from Google I applied the following code but the error is coming as given below
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[MetaWeather.Models.ApplicationUser]' while attempting to activate 'MetaWeather.Areas.Customer.Controllers.ForcastController'.
All-Star
52961 Points
23565 Posts
Re: Not getting the login user details from my controller
Jun 04, 2020 07:50 PM|mgebhard|LINK
The error means the Identity UserManager<ApplicationUser> is not registered with the DI framework. Are you using Identity?
You asked a similar question another thread where I provided the following...
private readonly ILogger<IndexModel> _logger; private readonly UserManager<IdentityUser> _userManager; public TheController(ILogger<TheController> logger, UserManager<IdentityUser> userManager) { _logger = logger; _userManager = userManager; }
Member
70 Points
22 Posts
Re: Not getting the login user details from my controller
Jun 05, 2020 06:12 AM|yinqiu|LINK
Hi polachan
Try to inject ApplicationUser into your Startup
Best regards
Yinqiu
Contributor
2690 Points
874 Posts
Re: Not getting the login user details from my controller
Jun 05, 2020 07:15 AM|Rena Ni|LINK
Hi polachan,
Be sure to register your ApplicationUser like below:
services.AddDefaultIdentity<ApplicationUser>(options =>{options.SignIn.RequireConfirmedAccount = true;}) .AddEntityFrameworkStores<ApplicationDbContext>();
Best Regards,
Rena