How to get current user in asp.net core

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

18 Answers
18

Leave a Comment