How to get current user in asp.net core
Asked 07 September, 2021
Viewed 2.1K times
  • 61
Votes

I want to get the current user, so I can access fields like their email address. But I can't do that in asp.net core. This is my code:

HttpContext almost is null in constructor of controller. It's not good to get a user in each action. I want to get the user's information once and save it to ViewData;

public DashboardController()
{
    var user = HttpContext.User.GetUserId();
}

15 Answer