How to get user id of logged in user in .NET Core 2.0?

Viewed 20615
5 Answers

Try this one:

var user = await userManager.GetUserAsync(HttpContext.User);
var ID = user.Id;
private readonly ApplicationDbContext _dbContext;
private readonly IHttpContextAccessor _httpContext;
private readonly string _userId;

public MyController(UserManager<ApplicationUser> _userManager, IHttpContextAccessor _httpContext)
{
    this._userManager = _userManager;
    this._httpContext = _httpContext;

    _userId = this._userManager.GetUserId(this._httpContext.HttpContext.User);
}
Related