locked
Not getting the login user details from my controller RRS feed

  • Question

  • User-1355965324 posted

    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);

    Thursday, June 4, 2020 7:25 PM

All replies

  • User475983607 posted

    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;
    
    }

    Thursday, June 4, 2020 7:50 PM
  • User-217098811 posted

    Hi polachan

    Try to inject ApplicationUser into your Startup

    Best regards

    Yinqiu

    Friday, June 5, 2020 6:12 AM
  • User711641945 posted

    Hi polachan,

    Be sure to register your ApplicationUser like below:

    services.AddDefaultIdentity<ApplicationUser>(options =>{options.SignIn.RequireConfirmedAccount = true;})
        .AddEntityFrameworkStores<ApplicationDbContext>();

    Best Regards,

    Rena

    Friday, June 5, 2020 7:15 AM